Josephus lottery

Professor Humbertov Moralov wants to make a raffle between the students of his Data Structure class and Pepito (a student of this group) suggests to use the Josephus problem to determine who is the winner of the raffle. The problem is that you can know beforehand the winning position if you know the value of n (the total of students in the raffle) and the value k (the amount of movements before throwing out a student from the circle). The prize is kind of interesting, the winner won’t have to take the final exam, and for that reason the professor Humbertov proposes the following variant to the Josephus problem: “Take the student class list, in which the students are numbered from 1 to n, then, organize these numbers in a circle and begin to count clockwise from number 1 to the value k. The student with number k in the list is removed from the circle, and now you begin to count, now counterclockwise, from the number of the next student (k + 1). The student with the number in which the count stopped is removed from the circle, and then you repeat the process alternating between clockwise and counterclockwise, counting until you get the winner of the raffle”. Input The input contains several test cases. Each test case has only one line, in which there are two positive integers N (1 ≤ N ≤ 106) and K (1 ≤ K ≤ N) that represents respectively, the number of students in the raffle and the value of movements to remove students from the circle. The input ends with a case containing two zeros, which must not be processed. Output For each test case you have to print the number in the student list that represents the winner of the raffle. Explication: This is the sequence for each step in the case “5 4”: 12345 123 5 1235 235 235 23 23 2 2 ← The winner Sample Input 10 1 10 5 10 10 55 54 00

2/2 Sample Output 6 2 5 4 2