Your company has N servers. The information flows from one server to another through a connection.
If the information flows from server i to server j, then connection(i) = j. It's possible for some server connection(i) = i, meaning information doesn't flow further.
You are given an array connection consisting of N integers. You are tasked with making a minimum number of changes to connection array values so that the information from all server can reach at exactly one server in the whole network, where it terminates.
If node X is terminal server, then connectioni = X.
Input Format
First-line contains an integer N, no of servers in the network.
Second line contains N integers, ith of which is connectioni.
Constraints
- 1 <= N <= 3*105
- 1 <= connectioni <= N
Output Format
An integer representing minimum number of changers required.
Sample Input 0
5
2 3 4 1 5
Sample Output 0
1
Explanation 0
We can choose node 5 as our terminal server and connect 4 -> 1 edge to 4->5. Our modified connection array becomes {2,3,4,5,5}
after making just 1 update.
Sample Input 1
4
1 2 3 4
Sample Output 1
3
We can select 1 as our terminal server and connect reset of the nodes to 1. The modified connection array becomes {1,1,1,1}
.