Jimmy has recently bought another large haul of jackboxes! To celebrate, he decides to give each of his jackboxes an integer from to and puts them all into a one-indexed array as a permutation of the integers . He decides that life is too boring just permuting the jackboxes, so he decides to play a game with his newly made array.
In this game, Jimmy can perform only perform one operation. When performing this operation, Jimmy will create a new array by doing the following:
- Given a value in his original array, the value at index in the new array is the value at the original array at index .
- He then replaces his original array with his new array.
The purpose of this game is to figure out, if possible, the number of operations it takes to return the array to its original state. Jimmy is a bit of a penguin brain, so could you, the newly recruited assistant of LCC Problems, help him solve this problem?
Constraints
Subtask 1 [5%]
Subtask 2 [95%]
No additional constraints.
Input Specification
The first line of input will contain an integer .
The second line contains space separated integers , where every number from to occurs once.
Output Specification
Output the number of operations it takes to return the array to its original value, or if it will never return.
Note that this number may not fit in a -bit integer.
Sample Input 1
3
2 3 1
Sample Output 1
2
Explanation for Sample 1
For the first operation, , so the new value of will be the value at index , which is . Likewise, and . After the operation, turns into . Performing the operation again, the value of the new array at index is , so , and likewise, and . AFter the second operation, turns into , returning us to the original array. Therefore, it takes operations.
Sample Input 2
4
1 2 4 3
Sample Output 2
-1
Explanation for Sample 2
After the first operation, the new array will be . Performing any operations on this array will loop back to , meaning that it is impossible to go back to the original array.
Comments