题库练习 Arpa's loud Owf and Mehrdad's evil plan
← 上一题 下一题 →

A10752 | Arpa's loud Owf and Mehrdad's evil plan

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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}$ .

![](/uploads/acgo/image/8e0091b1b3cba7c6_c2ebd947b00a.jpeg)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_{crushx}$ 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$ ).

输入格式

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.

输出格式

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

输入输出样例

输入 #1
4
2 3 1 4
输出 #1
3
输入 #2
4
4 4 4 4
输出 #2
-1
输入 #3
4
2 1 4 3
输出 #3
1
C++ 编辑器
输入
输出