Time limit
2s
Memory limit
128 MB
When designing a semiconductor, there are cases where n ports on one side must be connected to n ports on the other side. For each left-side port i, the number of the right-side port it should connect to is given.
You may choose only some of the connections. No two chosen connection lines may cross. Compute the maximum number of connections that can be chosen.
The first line contains an integer n (1 <= n <= 40,000).
The second line contains n integers. The i-th integer is the number of the right-side port that left-side port i must connect to. Each integer is between 1 and n, and no number appears more than once.
Print the maximum number of connections that can be chosen without crossings.