# Joseph

 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56348 Accepted: 21526

Description

The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3
4
0


Sample Output

5
30

 1 #include <stdio.h>
2
3 typedef int BOOL;
4
5 #define TRUE   0
6 #define FALSE  1
7
8 #define MAX_NUM 14
9
10 BOOL CheckM(int m, int k)
11 {
12     int i = 0, len = 2*k;
14
16     {
17         i = (i+m-1)%len;
18         if(i < k) return FALSE;
19         len--;
20         i %= len;
22     }
23     return TRUE;
24 }
25
26 int CalcM(int k)
27 {
28     int x, m = 0;
29
30     for(x = 0; x < 10000000; x++)
31     {
32         for(m = k+1+2*k*x; m <= 2*k*x+2*k; m++)
33         {
34             if(TRUE == CheckM(m, k)) return m;
35         }
36     }
37     return 0;
38 }
39
40 int main()
41 {
42     int k = 0;
43     int rst = 0;
44     int ans[MAX_NUM] = {0};
45
46     while(1 == scanf("%d", &k) && k > 0)
47     {
48         if(ans[k] == 0)
49         {
50             rst = CalcM(k);
51             ans[k] = rst;
52         }
53         else
54         {
55             rst = ans[k];
56         }
57         printf("%d\n", rst);
58     }
59
60     return 0;
61 }