#d2879. 传话游戏
传话游戏
The Whisper Game
Problem Description
There are people numbered to . Each person has a fixed whisper target: person will pass a message to person ( may equal ).
The game rules are as follows. Each round is started by some person . Person says to a string Oww...wwf containing letters w; if , then says to a string of the same form containing 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 () such that for every person the following holds: if a round started by has receiver , then a round started by must have receiver . If no such exists, output .
Input Format
The first line contains an integer (): the number of people.
The second line contains integers , where the -th number is the whisper target of person .
Output Format
Output the smallest ; if none exists, output .
Sample Input
4
2 3 1 4
Sample Output
3