program difficult;

var pole : array [0..2, 1..150000] of longint;
var max, i, j, hodnota, pocet : longint;

begin

while true do
begin

	readln(max);

	if max = 0 then
		break;

	pocet := 0;
	
	for i:=1 to max do
	begin
	
		read(pole[0][i]);
	
	end;
	
	readln;
	
	for j:= 1 to 2 do
	begin
	
		for i:=1 to max do
		begin
		
			read(hodnota);
			
			pole[j][hodnota] := i;
		
		end;
		
		readln;
	
	end;
	
	for i:=1 to max-1 do
	begin
	
		for j:= i+1 to max do
		begin
		
			if ((pole[1][pole[0][i]] < pole[1][pole[0][j]]) and (pole[2][pole[0][i]] < pole[2][pole[0][j]])) then
				inc (pocet);
		
		end; 
	
	end;
	
	writeln(pocet);
	
end;
	
	{
	for j:= 0 to 2 do
	begin
	
		for i:= 1 to max do
		begin
		
			write(pole[j][i],' ');
		
		end;
		
		writeln;
		
	end; 
	}

end.
