#B3805. Counting the Child Groups

    ID: 11462 Type: Default 1000ms 256MiB

Counting the Child Groups

Counting the Child Groups

During her duty in the computer room, E was overwhelmed by the noise of some children. To get some peace, she took N noisy children (numbered 1, 2, ..., N) out of the room. It turns out that these children form several small groups. Each child belongs to exactly one group. Every child is asked to state the leader of the group to which they belong. For the i-th child, the indicated leader's number is given as \( S_i \).

Assuming that all children are truthful, determine the total number of groups formed by these N children.

Note: In a valid configuration, a group leader will point to themselves (i.e. \( S_i = i \) for a leader). Every non-leader child will indicate the leader (a child whose \( S_j = j \)) of their group.

Input: The first line contains a positive integer N, representing the number of children. The second line contains N space-separated integers \( S_1, S_2, \dots, S_N \), where \( S_i \) is the indicated leader of the i-th child.

Output: Print a single integer representing the number of groups.

inputFormat

The input consists of two lines:

  1. The first line contains an integer N, the number of children.
  2. The second line contains N space-separated integers \( S_1, S_2, \dots, S_N \) where \( 1 \le S_i \le N \).

outputFormat

Output a single integer that represents the number of groups formed.

sample

3
1 1 3
2