Under construction

Still being built. Things may change

Joseph

Time limit1sMemory limit128 MB

Summary
For each k, find the smallest step size m in the Josephus circle that kills all k bad people before any of the k good people.
Level

Medium5 of 10

Topics
Simulation, Implementation, Brute force, Math
Solved
No attempts yet

Problem

The Josephus problem is famous. For those unfamiliar with the original: nn people numbered 1,2,…,n1, 2, \ldots, n stand in a circle, and every mm-th person is executed; only the last remaining person survives. Joseph was clever enough to choose the position of the last survivor, saving his own life so that he could later tell us about the incident. For example, when n=6n = 6 and m=5m = 5, the people are executed in the order 5,4,6,2,35, 4, 6, 2, 3, and person 11 survives.

Now suppose there are kk good people and kk bad people. In the circle the first kk are the good people and the last kk are the bad people. Determine the smallest mm such that every bad person is executed before the first good person.

Input

The input consists of several lines, each containing a single integer kk. The last line contains 00. You may assume that 0<k<140 < k < 14.

Output

For each kk in the input, print the corresponding mm on its own line.

Examples3

  1. Example 1

    Input
    3
    4
    0
    
    Expected output
    5
    30
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    2
    
  3. Example 3

    Input
    2
    0
    
    Expected output
    7