1. JAVA, Simon Baumgartner

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/*
* ACM Programming Contest
* Problem: 111 History Grading
* Status: Wrong Answer
* Runtime: 0.444
* Date: 2009-05-29
* Author: Simon Baumgartner
*
*/

public class Main {

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader in = new BufferedReader(new FileReader("input.txt"));

int nrOfEvents = Integer.parseInt(in.readLine());
String line = in.readLine();
line = line.replaceAll("( |\t)+", " ");
String[] nrs = line.split(" ");

int[] master = new int[nrOfEvents];
int[] order = new int[nrOfEvents];

for(int i = 0; i < nrs.length; i++)
master[i] = Integer.parseInt(nrs[i]);

// System.out.println("m: " + Arrays.toString(master));

while((line = in.readLine()) != null){
nrs = line.split(" ");
for(int i = 0; i< nrOfEvents; i++){
order[i] = Integer.parseInt(nrs[i]);
}
System.out.println(getScore(order, master));

}

}

private static String getScore(int[] order, int[] master) {
// System.out.println("f: " + Arrays.toString(order));

int[] a = new int[master.length];
int[] v = new int[master.length];
// v will hold the indexes of the master elements
for(int i = 0; i < master.length; i++)
for(int j = 0; j < order.length; j++)
if((master[i] == order[j]))
a[j] = i;
// System.out.println("A: " + Arrays.toString(a));
// 1 is the minimum distance at for each element
Arrays.fill(v, 1);
for(int i = 1; i < a.length; i++){
for(int j = 0; j < i; j++){
// if this is the correct order
if(a[j] < a[i] && v[j] + 1 > v[i]){
v[i] = v[j] + 1;
}
}
}
// System.out.println("V: " + Arrays.toString(v));
Arrays.sort(v);
// return maximum value
return v[v.length-1] + "";
}

}