Under construction

Still being built. Things may change

Counting-Out Rhyme

Interview

Time limit1sMemory limit128 MB

Summary
Given the order children leave a Josephus-style circle, find the smallest rhyme length k that produces exactly that leaving order, or report none exists.
Level

Medium6 of 10

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

Problem

Children stand in a circle and play a counting-out game. They are numbered 11 to nn so that, for each i=1,2,…,n−1i = 1, 2, \dots, n-1, child i+1i+1 stands immediately to the left of child ii, and child 11 stands immediately to the left of child nn. In other words, moving leftward around the circle the numbers increase 1,2,…,n1, 2, \dots, n and then wrap back to 11.

A single round of counting works as follows. Every round uses a rhyme of exactly kk syllables. The child who begins the round says the first syllable, the child to their left says the second, the next child to the left says the third, and so on, continuing leftward around the circle (a child may be reached more than once when kk is larger than the number of children still standing). The child who says the kk-th (last) syllable is counted out and leaves the circle.

  • The first round is begun by child 11.
  • Every later round is begun by the child immediately to the left of the child who was just counted out.

The counting is repeated, round after round, until no child remains.

Counting-out illustration

We watch a whole game and record the order in which the children left the circle. From this order alone we want to recover how many syllables the rhyme had. Given the leaving order, write a program that finds the smallest positive integer kk for which a kk-syllable rhyme makes the children leave in exactly that order, or reports that no such kk exists.

Input

The first line contains one integer nn (2≤n≤202 \le n \le 20). The second line contains nn integers separated by single spaces; the ii-th of them is the round in which child ii left the circle. These nn numbers form a permutation of 11 to nn.

Output

Print, on a single line, the smallest number kk of syllables the rhyme can have. If no such kk exists, print the word NIE instead.

Examples3

  1. Example 1

    Input
    4
    1 4 2 3
    
    Expected output
    5
    
  2. Example 2

    Input
    2
    1 2
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    2 1
    
    Expected output
    2