Josephus

Time Limit: 1000 ms

Memory Limit: 65535 ms

Description

The Josephus problem is the following game: N people, numbered 1 to N, are sitting in a circle. Starting at person 1, a hot potato is passed. After M passes, the person holding the hot potato is eliminated, and the game continues with the person who was sitting after the eliminated person picking up the hot potato. The last remaining person wins.
    Now, given N and the last remaining person's id, what is M?

Input

Every line has two integers: n(2 <= n <= 200) and id(1 <= id <= n). Input is terminated by EOF.

Output

For each input line, output one line containing M. If there are more than one M satisfying the request, output the smallest.

Sample Input

3 1
3 2

Sample Output

5
3

Hint

Source

The First ACM-ICPC Nanjing Invitational Tourn

提交代码