#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.
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 .