Manor Mystery


Submit solution

Points: 1
Time limit: 1.5s
Memory limit: 256M

Author:
Problem type
Allowed languages
C, C++, Java, Python

The gang has spotted a ghoulish ghost and are hot on its tail, but they need to cover their bases. This creepy critter is running as fast as he can, going in one room and through to the next, and isn't stopping. To catch him, the gang has decided to have the members stand guard, so that the ghoulish ghost will eventually run into them. What is the minimum number of members required to stand guard to that one of them will eventually encounter the frightening fiend?

The ghoulish ghost tears through the manor without stopping, entering room after room, always following the door ahead. Since the manor has finitely many rooms, the creepy critter must eventually find itself going in circles. The gang plans to station one member at each such loop, so that no matter which cycle the frightening fiend enters, someone will be waiting. What is the minimum number of members needed?

Input

The first line contains a single integer N (1 \leq N \leq 10^6) - the number of doors in the manor.

The second line contains N integers D_1\ \dots\ D_N (0\leq D_i \leq N - 1 ) - D_i is the room the ith door leads to, and all D_i are distinct.

Output

M - The number of members required to stand guard

Example

Input 1
4
1 2 0 3
Output 1
2

One member can be put on any of indices 0,1,2, and one on 3.


Comments

There are no comments at the moment.