PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000150. Tuyển chọn đội tuyển (Team Selection)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tuyển chọn đội tuyển (Team Selection)

Team Selection

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn contestants with identifiers 11 through nn, who take part in three competitions. Each competition provides one complete ranking, from best to worst. As there are no ties, every ranking is a permutation of 1,…,n1,\ldots,n.

Contestant AA is better than contestant BB if AA ranks ahead of BB in all three competitions. A contestant is excellent if there exists no other contestant better than them.

Count the excellent contestants. Only their number is required.

Input

The first line contains nn. Each of the next three lines is a permutation listing all contestant IDs from the first-ranked to the last-ranked in one competition.

Output

Print one integer: the number of excellent contestants.

Subtasks

  • Subtask 1 (20 points): 3≤n≤103\le n\le 10; each competition ranking is a complete permutation of 1,…,n1,\ldots,n with no ties.
  • Subtask 2 (30 points): 3≤n≤30003\le n\le 3000; each competition ranking is a complete permutation of 1,…,n1,\ldots,n with no ties.
  • Subtask 3 (50 points): 3≤n≤5000003\le n\le 500000; each competition ranking is a complete permutation of 1,…,n1,\ldots,n with no ties.

Examples

Example 1

Input

3
2 3 1
3 1 2
1 2 3

Output

3

Explanation

Rank triples are contestant 1: (3,2,1)(3,2,1), contestant 2: (1,3,2)(1,3,2), contestant 3: (2,1,3)(2,1,3). No contestant ranks ahead of another in all three contests, so all three are excellent.

Example 2

Input

4
1 2 3 4
1 2 3 4
1 2 3 4

Output

1

Explanation

Contestant 1 ranks first in all three competitions, beating contestants 2, 3, and 4 simultaneously. No contestant outranks contestant 1, so exactly one contestant is excellent.