If your team is lucky enough to advance to the ACM-ICPC World Finals, one of the situations you will face is the world finals competition itself.
At the beginning of each contest, every team tries to accomplish two separate goals:
To evaluate the performance of all teams in detail, we want to test these two goals separately. This problem deals with the former goal, finding the easiest problem; the latter goal, actually solving it, is analyzed in a separate problem.
The main trouble with comparing problem difficulty is that different people may have different opinions. To satisfy everyone, we need to find some consensus, so we start by determining all problems on which the opinions already agree.
Your team is given a set of ICPC problems. Each team member sorts all of the problems in order of their expected difficulty. We then want to find all pairs of problems whose relative order is the same according to all three orderings.
The input contains several tasks. Each task starts with one line containing a single integer $N$ ($2 \le N \le 150,000$), the number of problems to consider.
After that come three blocks, each describing the opinion of one team member (each team has three members). Every block specifies an arbitrary permutation of the numbers $1 \ldots N$ representing the problems; because it is a permutation, each number appears exactly once in each block.
Each block starts on a new line. For presentation reasons, the numbers inside a block may be split across any number of lines; when there is more than one number on a line, they are separated by at least one space. Empty lines may appear before and after blocks.
The last task is followed by a line containing a single zero.
For each task, print one line containing a single integer: the number of pairs of problems whose mutual order is the same in all three permutations.
Note that the result can be as large as $N \cdot (N-1)/2$ and may therefore exceed $2^{32}$.