CF741A.Arpa's loud Owf and Mehrdad's evil plan

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

People in Arpa's land are numbered from 11 to nn . Everyone has exactly one crush, ii -th person's crush is person with the number crushicrush_{i} .

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 xx wants to start a round, he calls crushxcrush_{x} and says: "Oww...wwf" (the letter w is repeated tt times) and cuts off the phone immediately. If t>1 then crushxcrush_{x} calls crushcrushxcrush_{crushx} and says: "Oww...wwf" (the letter w is repeated t1t-1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" ( t=1t=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 tt ( t>=1t>=1 ) such that for each person xx , if xx starts some round and yy becomes the Joon-Joon of the round, then by starting from yy , xx would become the Joon-Joon of the round. Find such tt for Mehrdad if it's possible.

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

输入格式

The first line of input contains integer nn ( 1<=n<=1001<=n<=100 ) — the number of people in Arpa's land.

The second line contains nn integers, ii -th of them is crushicrush_{i} ( 1<=crushi<=n1<=crush_{i}<=n ) — the number of ii -th person's crush.

输出格式

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

输入输出样例

  • 输入#1

    4
    2 3 1 4
    

    输出#1

    3
    
  • 输入#2

    4
    4 4 4 4
    

    输出#2

    -1
    
  • 输入#3

    4
    2 1 4 3
    

    输出#3

    1
    

说明/提示

In the first sample suppose t=3t=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 xx is 11 .

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 xx is 44 .

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

首页