#d2879. 传话游戏

传话游戏

The Whisper Game

Problem Description

There are nn people numbered 11 to nn. Each person has a fixed whisper target: person ii will pass a message to person aia_i (aia_i may equal ii).

The game rules are as follows. Each round is started by some person xx. Person xx says to axa_x a string Oww...wwf containing tt letters w; if t>1t > 1, then axa_x says to aaxa_{a_x} a string of the same form containing t−1t - 1 letters w; and so on, until someone hears a string with exactly one w, namely Owf — that person is the receiver of this round. (At any moment, at most one round is in progress.)

Find the smallest tt (t≥1t \geq 1) such that for every person xx the following holds: if a round started by xx has receiver yy, then a round started by yy must have receiver xx. If no such tt exists, output −1-1.

Input Format

The first line contains an integer nn (1≤n≤1501 \le n \le 150): the number of people.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n, where the ii-th number is the whisper target aia_i of person ii.

Output Format

Output the smallest tt; if none exists, output −1-1.

Sample Input

4
2 3 1 4

Sample Output

3