402511: GYM100796 E Permutation Polygon

Memory Limit:256 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Permutation Polygontime limit per test0.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

There are n cities in Regularia, Alice's homeland. They are located at the vertices of a regular n-gon. The cities are numbered from 1 to n in a clockwise order (see the figure).

The people of Regularia love Alice so much that they made her the queen of the country. Alice wants to prove herself, so she has ordered a construction of a new highway system connecting cities of Regularia. Alice has come up with a plan to build the highways, which is an n-permutation p1, p2, ..., pn. Specifically, for a city numbered i, Alice wants to build a highway between the cities i and pi, where pi is the number of some other city. A highway is a straight line connecting two cities. According to Alice's plan, any two cities will be connected by no more than a single highway.

Of course, some of the highways may intersect one another. Two highways are said to intersect if and only if there is a unique point that belongs to both of the highways and is not a city. This point is called a junction of the two highways. Even if more than two highways meet at a single point, each pair has its own separate junction. As a complicated and expensive interchange has to be built at each junction, Alice is interested in the total number of junctions in her plan. Help her find this number!

For example, the plan for a permutation 4, 1, 5, 3, 2 is shown in the figure.

Input

The first line contains a single integer n, the number of cities in Regularia (3 ≤ n ≤ 105). The next line contains n space-separated integers p1, p2, ..., pn, which is a permutation of length n. It is guaranteed that i ≠ pi for any i and that no two cities will be connected by more than a single highway.

Output

Output a single integer — the total number of junctions in Alice's plan.

ExamplesInput
5
4 1 5 3 2
Output
2
Input
6
4 5 6 3 1 2
Output
6
Note

A permutation of length n is any rearrangement of the sequence 1, 2, ..., n.

加入题单

算法标签: