This page is still under construction.

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

The Last Battle

Interview

Time limit1sMemory limit512 MB

Summary
A permutation a is fixed; rotating the identity arrangement right by k positions must satisfy a[i] != shifted value at each position. Find the smallest valid k, or -1.
Level

Medium6 of 10

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

Problem

Soon there will be a final battle between people and Martians. Spies of people found out that the Martians had nn soldiers left. It also turned out that people as well as the Martians had exactly nn fighters.

According to the experience of past battles, people know that only one Martian with the number ii can defeat an ii-th person.

The commander decided to build in a line of people. Learning Martians plans, the commander found out that a man with ii-th position in the line will fight with Martian number aia_i. People will win only if each of the fighters is guaranteed to win in their battle.

Firstly commander put the ii-th person on the ii-th position int the line. After that, he realized that he had little time before the battle, and that people can lose if the line is not rebuilt. In one second he can move a person from the last place to the beginning of the line, after this operation this fighter is in the first position, and the position number of each of the other fighters is increased by 11.

Help him to find out the minimal time he can rebuild the line so that people will win.

Input

The first line contains an integer nn --- the number of fighters of each side (1≤n≤2⋅1051 \le n \le 2\cdot10^5).

The second line contains nn distinct integers a1,a2,…,ana_1, a_2, \ldots, a_n, where aia_i is the number of the Martian with whom the person from the ii-th position in the line will fight (1≤ai≤n1 \le a_i \le n, if i≠ji \ne j, then ai≠aja_i \ne a_j).

Output

Output a single number kk --- the minimum number of seconds for which the commander can rebuild the line so that people will win. If people can not defeat the Martians, print the number <<−1-1>>.

Hint

In the first example, initially the fighters stand opposite each other in the following way:

Martians:14235People:12345\begin{matrix} \text{Martians:}&1&4&2&3&5\\ \text{People:} &1&2&3&4&5\\ \end{matrix}

People lose, as the Martians number 11 and 55 win their fights. After the first move, the line of the fighters changes to this line:

Martians:14235People:51234\begin{matrix} \text{Martians:}&1&4&2&3&5\\ \text{People:} &5&1&2&3&4\\ \end{matrix}

Now the Martians 22 and 33 win their battle, so commander need to move the last person to the beginning of the line again. After it, the line of the fighters becomes such that all people win their fight.

Martians:14235People:45123\begin{matrix} \text{Martians:}&1&4&2&3&5\\ \text{People:} &4&5&1&2&3\\ \end{matrix}

Examples2

  1. Example 1

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

    Input
    5
    1 3 5 2 4
    
    Expected output
    -1