atcoder#AGC037C. [AGC037C] Numbers on a Circle
[AGC037C] Numbers on a Circle
Score : points
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.
3
1 1 1
13 5 7
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 .
4
1 2 3 4
2 3 4 5
-1
5
5 6 5 2 1
9817 1108 6890 4343 8704
25