eolymp
bolt
Try our new interface for solving problems
Problems

Why Did the Cow Cross the Road III (Gold)

Why Did the Cow Cross the Road III (Gold)

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn.

As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns n cows, conveniently identified with the integer IDs 1 .. n, so there are precisely 2n crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2n numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a, b) a "crossing" pair if cow a's path from entry to exit must cross cow b's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

Input

The first line contains n (1n50000), and the next 2n lines describe the cow IDs for the sequence of entry and exit points around the field.

Output

Please print the total number of crossing pairs.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4
3
2
4
4
1
3
2
1
Output example #1
3
Source 2017 USACO February, Gold