cho.sh
Notes
Loading...

Shuffle Sequence

Time limit

1s

Memory limit

128 MB

Problem

There are N cards initially arranged as [A1, A2, ..., AN]. A shuffle method is given as a sequence of length N containing each number from 1 through N exactly once. One shuffle builds a new deck by taking, in order, the cards currently located at the positions written in the sequence.

For example, if N = 6 and the sequence is [3, 2, 5, 6, 1, 4], the initial order [A1, A2, A3, A4, A5, A6] becomes [A3, A2, A5, A6, A1, A4] after one shuffle. Repeating the same shuffle eventually returns the deck to its initial order.

The orbit of a shuffle sequence is the minimum number of shuffles needed to return to the initial order. Given a shuffle sequence, compute its orbit.

Input

The first line contains the number of cards N. (1 <= N <= 20,000)

The second line contains N positive integers separated by spaces, representing the shuffle sequence. Each number from 1 through N appears exactly once.

Output

Print the orbit of the given shuffle sequence on the first line. Every input has an orbit between 1 and 2,000,000,000, inclusive.