Selecting the capital

Given a connected multigraph where city i links to city R[i], merge adjacent cities the fewest times so that some city lies on every simple path between any two cities.

Medium7GraphMinimum spanning treeGreedyImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

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 RiR_i of length N: for each ii there is one road joining city ii and city RiR_i, 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 RiR_i, write a program that finds the smallest number of merges needed to choose a capital.

Input

The first line contains N. The second line contains R0R_0 through RN1R_{N-1} in order. (2N502 \le N \le 50, 0RiN10 \le R_i \le N-1)

Output

Print the smallest number of merges needed to choose a capital.