Two people are playing a game with a pile of matches.
In each turn, and player may take between 1 and a given maximal number of matches. The winner is the one that takes the last match.
For example, if the game pile contains 10 matches and it is possible to take up to three matches. The first player takes 3, leaving 7. The second takes 3 leaving 4 matches, The first than takes 2 leaving 2, followed by the second player taking 2 matches leaving none, so he is the winner.
Your question is: Assuming both players play the game perfectly, what is the move that the first player needs to make? (specifically, how many matches should be taken?)
The input contains two numbers, the first being the number of matches in the pile, and the second is the maximal number of matches that may be taken in each round. For example, given the numbers 4 and 2, the first player should take 1 match to assure winning. (You may check this at your leisure). If the first player had no winning move, 0 should be printed.
You must be