#P5944. [POI 2002] 出圈游戏

[POI 2002] 出圈游戏

Problem Description

There are nn children numbered from 11 to nn playing a circle elimination game. Child i+1i+1 stands to the left of child ii. Child 11 stands to the left of child nn. First, child 11 starts counting, then the child on the left continues counting in order. Whenever the count reaches a certain number KK, that child leaves the circle. The game ends when all children have left the circle.

Now you are given the elimination order. Find the smallest KK.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers aia_i. The ii-th integer means that the child numbered ii is the aia_i-th to leave the circle.

Output Format

Output the smallest KK. If it does not exist, output the word NIE.

4
1 4 3 2
NIE
4
1 4 2 3
5

Hint

For 100%100\% of the testdata, 2n202 \le n \le 20.


upd 2022.8.24\text{upd 2022.8.24}: A new set of hack testdata has been added.

Translated by ChatGPT 5