P27298 [AtCoder] Lying Angels
提高-
题目描述

Problem Description

Angels have infiltrated by demons. You must report this to your boss, but your boss has ordered you to "investigate the maximum number of demons that could have infiltrated."

"Who do you think is the demon?"

When you asked, they pointed at each other, saying "He is the demon!" No one pointed at the same person; everyone pointed at different individuals.

It seems that angels always point at demons, and demons always point at angels.

What is the maximum number of demons that could be among them? Please report that number.

However, you and your boss are neither angels nor demons and are not included in the "they" given in the input. Each of "they" is either an angel or a demon.

Input Format

The input is given from the standard input in the following format:

$ N $ $ A_{1} $ $ A_{2} $ $ : $ $ A_{N} $

A single integer $ N $ followed by $ N $ lines of integers.

The $ i+1 $th line indicates that the person numbered $ i $ pointed at the person numbered $ A_{i} $.

However, $2\ \leq\ N\ \leq\ 10^5$, $1\ \leq\ A_{i}\ \leq\ N$, and for any distinct $ i, $ $ j $, $ A_i≠A_j $.

Output Format

Output the maximum number of demons that could be considered in one line.

If any assignment of angels and demons leads to a contradiction, output $ -1 $.

Sample Input and Output

Sample Input #1

4
2
3
4
1

Sample Output #1

2

Sample Input #2

3
2
3
1

Sample Output #2

-1

Notes/Hints

None

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 2 3 4 1
样例输出
2