PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000017. PolandBall và khu rừng (PolandBall and Forest)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

PolandBall và khu rừng (PolandBall and Forest)

PolandBall and Forest

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A forest consists of several disjoint trees with a total of nn vertices numbered from 11 to nn. For every vertex ii, you are given the vertex pip_i farthest from ii in the same tree. If several vertices attain the maximum distance, pip_i is the one with the smallest index.

Determine the number of trees from the sequence p1,p2,…,pnp_1,p_2,\ldots,p_n.

Input

The first line contains an integer nn. The next line contains nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n. It is guaranteed that at least one forest satisfies the given information.

Output

Print the number of trees in the forest.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1501\le n\le 150; 1≤pi≤n1\le p_i\le n for all 1≤i≤n1\le i\le n; the sequence describes a valid forest.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; 1≤pi≤n1\le p_i\le n for all 1≤i≤n1\le i\le n; the sequence describes a valid forest.
  • Subtask 3 (50 points): 1≤n≤100001\le n\le 10000; 1≤pi≤n1\le p_i\le n for all 1≤i≤n1\le i\le n; the sequence describes a valid forest.

Examples

Input

5
2 1 5 3 3

Output

2

Explanation

The farthest-vertex array is consistent with components {1,2} and {3,4,5}, so there are two trees.