Codeforces Round 383 (Div. 1)
A
Arpa's loud Owf and Mehrdad's evil plan
Codeforces
1600
原题
←B →
翻译中…

As you have noticed, there are lovely girls in Arpa’s land.

翻译中…

People in Arpa's land are numbered from $1$ to $*n*$. Everyone has exactly one crush, $*i*$-th person's crush is person with the number $*crush*_{*i*}$.

翻译中…

image

翻译中…

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

翻译中…

The game consists of rounds. Assume person $*x*$ wants to start a round, he calls $*crush*_{*x*}$ and says: "Oww...wwf" (the letter w is repeated $*t*$ times) and cuts off the phone immediately. If $*t* > 1$ then $*crush*_{*x*}$ calls $*crush*_{*crush*_{*x*}}$ and says: "Oww...wwf" (the letter w is repeated $*t* - 1$ times) and cuts off the phone immediately. The round continues until some person receives an "Owf" ($*t* = 1$). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

翻译中…

Mehrdad has an evil plan to make the game more funny, he wants to find smallest $*t*$ ($*t* ≥ 1$) such that for each person $*x*$, if $*x*$ starts some round and $*y*$ becomes the Joon-Joon of the round, then by starting from $*y*$, $*x*$ would become the Joon-Joon of the round. Find such $*t*$ for Mehrdad if it's possible.

翻译中…

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. $*crush*_{*i*} = *i*$).

翻译中…

Input

翻译中…

The first line of input contains integer $*n*$ ($1 ≤ *n* ≤ 100$) — the number of people in Arpa's land.

翻译中…

The second line contains $*n*$ integers, $*i*$-th of them is $*crush*_{*i*}$ ($1 ≤ *crush*_{*i*} ≤ *n*$) — the number of $*i*$-th person's crush.

翻译中…

Output

翻译中…

If there is no $*t*$ satisfying the condition, print -1. Otherwise print such smallest $*t*$.

翻译中…

Note

翻译中…

In the first sample suppose $*t* = 3$.

翻译中…

If the first person starts some round:

翻译中…

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if $*x*$ is $1$.

翻译中…

The process is similar for the second and the third person.

翻译中…

If the fourth person starts some round:

翻译中…

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when $*x*$ is $4$.

翻译中…

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.

翻译中…

样例

Input #1
4
2 3 1 4
Output #1
3
InputCopy OutputCopy
Input #2
4
4 4 4 4
Output #2
-1
InputCopy OutputCopy
Input #3
4
2 1 4 3
Output #3
1
InputCopy OutputCopy
题目信息
题号
741A
时间限制
1 s
内存限制
256 MB
标签
DFS 及类似
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。