#agc037c. [agc037_c]Numbers on a Circle
[agc037_c]Numbers on a Circle
Problem Statement
There are positive integers arranged in a circle.
Now, the -th number is . Takahashi wants the -th number to be . For this objective, he will repeatedly perform the following operation:
- Choose an integer such that .
- Let be the -th, -th, and -th numbers, respectively. Replace the -th number with .
Here the -th number is the -th number, and the -th number is the -st number.
Determine if Takahashi can achieve his objective. If the answer is yes, find the minimum number of operations required.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of operations required, or -1
if the objective cannot be achieved.
Sample Input 1
3
1 1 1
13 5 7
Sample Output 1
4
Takahashi can achieve his objective by, for example, performing the following operations:
- Replace the second number with .
- Replace the second number with .
- Replace the third number with .
- Replace the first number with .
Sample Input 2
4
1 2 3 4
2 3 4 5
Sample Output 2
-1
Sample Input 3
5
5 6 5 2 1
9817 1108 6890 4343 8704
Sample Output 3
25