The Last Battle
InterviewTime limit1sMemory limit512 MB
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 soldiers left. It also turned out that people as well as the Martians had exactly fighters.
According to the experience of past battles, people know that only one Martian with the number can defeat an -th person.
The commander decided to build in a line of people. Learning Martians plans, the commander found out that a man with -th position in the line will fight with Martian number . People will win only if each of the fighters is guaranteed to win in their battle.
Firstly commander put the -th person on the -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 .
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 --- the number of fighters of each side ().
The second line contains distinct integers , where is the number of the Martian with whom the person from the -th position in the line will fight (, if , then ).
Output
Output a single number --- 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 <<>>.
Hint
In the first example, initially the fighters stand opposite each other in the following way:
People lose, as the Martians number and win their fights. After the first move, the line of the fighters changes to this line:
Now the Martians and 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.