Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m.
What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?
Input
The single line contains two space separated integers n, m (0 < n ≤ 10000, 1 < m ≤ 10).
Output
Print a single integer — the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print - 1 instead.
Examples
Input
10 2
Output
6
Input
3 5
Output
-1
Note
For the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.
For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: Given n steps, we can climb 1 or 2 steps each move. Let x be number of moves. The total steps sum = n, with each step either 1 or 2. So if we let a = number of 1-step moves, b = number of 2-step moves, then a + 2b = n, and total moves x = a + b. Also a,b ≥ 0 integers.
We need x to be a multiple of m, i.e., x % m == 0. And we want minimal such x (i.e., minimal number of moves) that satisfies the equation.
If no such x exists, output -1.
Constraints: n up to 10000, m up to 10.
Simple approach: Since n is small, we could iterate over possible b from 0 to floor(n/2). Then a = n - 2b, x = a + b = n - b. Because x = a+b = (n-2b) + b = n - b. So as b increases, x decreases. So minimal number of moves corresponds to maximizing b (the number of 2-step moves). Since each move can be 2 steps, to minimize number of moves, we want as many 2-step moves as possible. So the minimum possible number of moves (without m constraint) is ceil(n/2) with constraints? Actually minimal moves is achieved by using as many 2's as possible: if n even, min = n/2; if n odd, min = floor(n/2) + 1 = (n+1)/2. That corresponds to b = floor(n/2), a = n%2. So x_min = ceil(n/2) but also it's n - b_max, where b_max = floor(n/2). So x_min = n - floor(n/2) = ceil(n/2). Indeed.
Now we need x such that x % m == 0, and x >= ceil(n/2) (since we need at least that many moves) and also x <= n (when all moves are 1-step, x = n). So x is in [ceil(n/2), n]. Also x = n - b for some integer b in [0, floor(n/2)], so x can take only specific values: x = n, n-1, n-2, ..., n - floor(n/2) = ceil(n/2). So x possible values are those congruent to n mod 1? Actually step size: decreasing by 1 each time? Because b increments by 1 reduces x by 1? Let's check: x = n - b, b integer. So as b increases by 1, x decreases by 1. So all integers from ceil(n/2) to n inclusive are possible? Not necessarily: For n=1: floor(1/2)=0, so only b=0 => x=1. Ceil(1/2)=1, so range is [1,1] OK. For n=2: floor(2/2)=1, b=0 => x=2, b=1 => x=1? Wait: b=1 gives a = n-2b = 0, x = 0+1=1. But does x=1 correspond to a single move of 2 steps? That's valid: one move of 2 steps, total moves =1, steps sum=2. So x can be 1, which is less than ceil(2/2)=1? Actually ceil(2/2)=1. So range is from 1 to 2. So all integers from 1 to n are possible? Let's test n=3: floor(3/2)=1. b=0 => a=3,x=3; b=1 => a=1,x=2. So possible x: 2 and 3. Ceil(3/2)=2, range from 2 to 3 includes both. So all integers from ceil(n/2) to n are possible? For n=4: b=0=>x=4; b=1=>a=2=>x=3; b=2=>a=0=>x=2. Ceil(4/2)=2, range 2..4 includes all. For n=5: b=0=>5; b=1=>a=3=>4; b=2=>a=1=>3. Ceil(5/2)=3, range 3..5 includes 3,4,5. So indeed, all integers from ceil(n/2) to n inclusive are achievable. Why? Because as we vary b from 0 to floor(n/2), x = n - b decreases by 1 each step, covering all integers from n - floor(n/2) = ceil(n/2) to n. Yes.
Thus the problem reduces to: Find the smallest integer x in the interval [ceil(n/2), n] such that x % m == 0. If none exists, output -1.
But is there any other constraint? For m > 1? For m=1, any x works, but m>1 per input: 1 < m ≤ 10. So m at least 2. So we can just compute ceil(n/2) = (n+1)//2 integer division? Actually ceil(n/2) = (n+1)//2 in integer arithmetic for positive n.
Then we need to find the smallest multiple of m >= ceil(n/2) and <= n. That is: let L = ceil(n/2). Compute the smallest k such that km >= L and km <= n. Then answer = k*m if such exists, else -1.
But careful: Is the minimal number of moves indeed the smallest x in that range that is multiple of m? Because we want minimal number of moves satisfying condition. Since x decreases as we use more 2-steps, the smallest possible x is the minimal moves. Among all multiples of m, the smallest x that is feasible is the smallest multiple >= L. Since larger multiples are larger moves, we want the smallest feasible x. So yes.
But wait: Could there be a scenario where using more moves (i.e., larger x) somehow results in a multiple of m, but the minimal x (multiple) is actually larger than the smallest multiple? Actually we want minimal number of moves, so we want the smallest x. So we need the smallest x in [L, n] that is divisible by m. That is indeed the first multiple of m >= L, provided it is <= n.
Thus answer = ((L + m - 1) // m) * m, if that <= n, else -1.
But we must consider a nuance: The number of moves x must be a multiple of m. But is there any hidden condition like "the number of moves to be a multiple of an integer m" meaning exactly multiple? Usually yes. So above is fine.
Let's test sample: n=10, m=2. L = ceil(10/2)=5. smallest multiple of 2 >=5 is 6. 6 <=10, output 6. matches.
Second sample: n=3, m=5. L= ceil(3/2)=2. smallest multiple of 5 >=2 is 5. But 5 > n=3, so no, output -1. matches.
So solution seems trivial.
But wait: Are we missing any other constraints? Input says 0 < n ≤ 10000, 1 < m ≤ 10. So m up to 10, small. Works.
But is it always true that all integers in [L, n] are achievable? We argued yes. But we should double-check for edge cases: n=0 is not allowed. n=1: L=1, n=1 -> only x=1. Good. n=2: L=1, n=2 -> both 1,2. Achievable? x=1 -> one move of 2 steps. x=2 -> two moves of 1 each. So yes. So it's fine.
Thus solution is O(1). But we need to print -1 if no such multiple.
But the problem statement says "If there is no way he can climb satisfying condition print -1 instead." So that's correct.
Now, is there any scenario where the minimal number of moves is not the smallest multiple? Actually, maybe there is a nuance: The moves must be a multiple of m, but we are minimizing number of moves. Since number of moves can be any integer between L and n inclusive, the minimal one that is a multiple of m is indeed the smallest multiple in that range.
But what if the minimal moves (L) itself is not achievable due to parity? For example, n=3, L=2, which is achievable as we saw (2 moves: 2+1). So it's fine.
So solution: read n, m; compute L = (n+1)//2; compute x = ((L + m - 1) // m) * m; if x <= n print x else print -1.
Wait: But note: (L + m -1) // m gives ceiling division of L by m. Multiply by m yields the smallest multiple of m >= L. Good.
But is it possible that the minimal moves is not the smallest multiple because we can also adjust moves by adding extra pairs of (1,1) maybe? Actually all moves are 1 or 2. Adding extra moves means increasing total moves, but we are looking for minimal number of moves. So smaller x is better. So the smallest multiple of m >= L is the minimal possible, if it's within n. So fine.
Thus answer is trivial.
But I recall a known Codeforces problem: "Climbing Stairs" with m multiple? Actually it's from Codeforces Round #... Possibly problem 476B or something? But the solution there might be different? Actually there is a problem: "Dreamoon and Stairs" (Codeforces 476A). I think this is exactly Codeforces 476A: "Dreamoon and Stairs". In that problem, the solution is: find minimal x such that x >= ceil(n/2) and x % m == 0. Exactly.
But maybe there is a twist: In that problem, m can be up to 10? Actually m up to 100? Anyway it's the same.
Thus the answer is straightforward.
Now we should produce final answer: Provide explanation and code.
But the user asked: "Dreamoon wants to climb up a stair of n steps..." So they likely want solution code in some language? Not specified. But we can provide reasoning and final answer. Since it's a coding problem, we can propose a Python solution.
Thus in final answer: Provide description of solution: Minimum moves is at least ceil(n/2). The number of moves can be any integer from ceil(n/2) to n inclusive. So find smallest multiple of m in that range. If none, -1.
We should also note that we can derive directly: x = ((n+1)//2 + m - 1) // m * m, then check.
Now produce final answer with code.
But the platform may expect only answer (like a number) not code. The problem statement says "Print a single integer". So if we output just the number, that's fine. However, the user may be expecting a solution explanation and code. Since they posted the problem description, it's typical for a coding platform. So we can produce a solution in Python.
Thus final answer:
python
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n, m = map(int, data)
L = (n + 1) // 2
x = ((L + m - 1) // m) * m
if x <= n:
print(x)
else:
print(-1)
if __name__ == "__main__":
solve()
But ensure that input parsing handles multiple spaces.
Thus answer: Explanation and code.
But the assistant must output only the final answer? The user's message: "Dreamoon wants to climb up a stair ..." It's a problem statement, and the assistant should respond with solution? Probably they want the answer. But typical ChatGPT interactions: they provide problem, we give solution and code. So we can include both.
To be safe, I'll provide reasoning and then code.
Now I'll produce final answer. The minimal number of moves is the smallest integer (x) in the range (\lceil n/2 \rceil \le x \le n) such that (x) is a multiple of (m).
If no such (x) exists, output (-1).
python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n, m = map(int, data)
L = (n + 1) // 2 # minimal moves (ceil(n/2))
x = ((L + m - 1) // m) * m # smallest multiple of m >= L
if x <= n:
print(x)
else:
print(-1)
if __name__ == "__main__":
main()