This page is still under construction.

Parts of this page are still being built. What you see may change.

In Danger

Time limit1sMemory limit128 MB

Summary
Given n people in a circle where every second person is eliminated starting from person 1, report the final surviving position; n arrives encoded as xyez.
Level

Medium5 of 10

Topics
Math, Recursion, Simulation, Bit manipulation
Solved
No attempts yet

Problem

Flavius Josephus and 40 of his fellow rebels were trapped and surrounded by the Romans. His companions preferred suicide to surrender, so they decided to stand in a circle and, going around it, kill every third person until no one was left. Josephus did not want to die, so he calculated in advance the position that would be the last one standing (and, since no one was left to watch, he did not kill himself).

Here we consider a variant of this game in which every second person leaves the circle. Because we now have computers, there can be far more than 41 participants. Compute the safe position. Be careful: we might run your program to determine the winner of this contest!

Input

The input consists of several test cases. Each case gives the number of participants nn. To make things harder, nn is always given in the format xyez, with the following meaning: when nn is written in decimal, its first digit is xx, its second digit is yy, and then follow zz zeros. That is, n=(10x+y)⋅10zn = (10x + y)\cdot 10^z. Here 0≤x,y≤90 \le x, y \le 9 and the number of zeros satisfies 0≤z≤60 \le z \le 6. It is guaranteed that n>0n > 0. The last test case is followed by the string 00e0.

Output

For each test case, print on its own line the position of the person who survives. The participants are numbered from 11 to nn, and counting starts at person 1, so the first person to leave is number 2. For example, if there are 5 people in the circle, the counting proceeds as 2,4,1,52, 4, 1, 5 and person 3 stays alive.

Examples1

  1. Example 1

    Input
    05e0
    01e1
    42e0
    66e6
    00e0
    
    Expected output
    3
    5
    21
    64891137