import java.io.*;

public class difficult {
	
	
	
	public static void main(String[] args) throws Exception {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String l;
		String[] p;
		boolean[][] xxx;
		int[] line;
		int n, x, c;
		while(true) {
			l = br.readLine();
			if(l.equals("0")) break;
			if(l.trim().equals("")) continue;
			
			n = Integer.parseInt(l);
			xxx = new boolean[n+1][n+1];
			line = new int[n];
			for(int t = 0; t<3; t++) {
				c = 0;
				while(c<n) {
					l = br.readLine();
					p = l.split("\\s+");
					for(int i = 0; i<p.length; i++) {
						x = Integer.parseInt(p[i]);
						line[c] = x;
						c++;
					}
				}
				
				if(t == 2) break;
				for(int i = 0; i<n; i++) {
					for(int j = i+1; j<n; j++) {
						xxx[line[j]][line[i]] = true;
					}
				}
			}
			int count = 0;
			for(int i = 0; i<n; i++) {
				for(int j = i+1; j<n; j++) {
					if(xxx[line[i]][line[j]] == false)  count++;
				}
			}
			System.out.println(count);
			
		}
	}
	
	
	
	
	
	
	public static void _main(String[] args) throws Exception {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String l;
		String[] p;
		int n, x;
		int minmax[][];
		int c;
		int count;
		while(true) {
			l = br.readLine();
			if(l.equals("0")) break;
			if(l.trim().equals("")) continue;
			
			n = Integer.parseInt(l);
			minmax = new int[n+1][2];
			for(int i = 0; i<minmax.length; i++) {
				minmax[i][1] = -1;
				minmax[i][0] = Integer.MAX_VALUE;
			}
			for(int t = 0; t<3; t++) {
				c = 0;
				while(c<n) {
					l = br.readLine();
					System.out.println("line:"+l);
					p = l.split("\\s+");
					for(int i = 0; i<p.length; i++) {
						x = Integer.parseInt(p[i]);
						minmax[x][0] = Math.min(c, minmax[x][0]);
						minmax[x][1] = Math.max(c, minmax[x][1]);
						c++;
					}
				}
			}
			count = 0;
			for(int i = 1; i<minmax.length; i++) {
				System.out.println(minmax[i][0]+" "+minmax[i][1]);
				for(int j = i+1; j<minmax.length; j++) {
					if(minmax[i][1]<minmax[j][0]) count++;
				}
			}
			System.out.println(count);
		}
		
	}

}