#P6487. [COCI 2010/2011 #4] HRPA

[COCI 2010/2011 #4] HRPA

Description

There are nn stones. Two players play the game with the following rules:

  • Mirko takes once first, then Slavko takes once, then Mirko takes once again. They take turns taking stones, and so on.
  • On Mirko’s first move, he may take any positive number of stones.
  • After that, on each move, the player must take at least one stone and at most 22 times the number taken in the previous move. Of course, the number taken must not exceed the number of stones currently remaining on the table.
  • The player who takes the last stone wins.

Both players play optimally. Mirko wants to know the minimum number of stones he must take on his first move in order to win in the end.

Input Format

One line containing one integer nn, the number of stones.

Output Format

One line containing one integer, the minimum number of stones Mirko must take to guarantee a win.

4
1
7
2
8
8

Hint

Explanation of Sample 1

For this sample, Mirko can take 1/2/3/41/2/3/4 stones on his first move. Although taking 44 stones would win the game immediately, it is not the minimum. The minimum strategy is to take 11 stone. Then Slavko can only take 11 or 22 stones. No matter which one he chooses, Mirko can take all remaining stones on the next move and win.

Constraints

For 100%100\% of the testdata, it is guaranteed that 2n10152\le n\le 10^{15}.

Note

Translated from COCI2010-2011 CONTEST #4 T6 HRPA

Translated by ChatGPT 5