A country has N cities and N two-way roads. The cities are numbered from 0 to N-1. The roads are described by a sequence Ri of length N: for each i there is one road joining city i and city Ri, so a pair of cities can be joined by two roads. Any two cities can always reach each other over the roads.
The president wants to make one city the capital. The capital has to satisfy the following condition.
- However two different cities A and B are chosen, every simple path from A to B passes through the capital.
If no city satisfies the condition, a new city has to be built. A new city is built by merging two neighboring cities, and merging can be done several times. One merge works like this.
- Pick two cities X and Y that a road joins. By the rule above, more than one road can run between the two cities.
- Combine the two cities into a new city Z.
- Every road between X and Y disappears, and every other city that was joined to X or to Y becomes joined to Z.
For example, look at the graph below.

In this situation no capital can be chosen. Merging city 1 and city 2 gives the graph below.

The new city can be the capital.
Given N and Ri, write a program that finds the smallest number of merges needed to choose a capital.