#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 30;
const int inf = 1<<30;
int n;
int str1[maxn],str2[maxn],dp[maxn][maxn];
void GetDp()
{
memset( dp,0,sizeof(dp) );
for( int i = 1; i <= n; i ++ )
for( int j = 1; j <= n; j ++ )
if( str1[i] == str2[j] ) dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max( dp[i-1][j],dp[i][j-1] );
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int tmp;
scanf("%d",&n);
for( int i = 1; i <= n; i ++ ){
scanf("%d",&tmp);
str1[tmp] = i;
}
while( scanf("%d",&tmp ) != EOF )
{
str2[tmp] = 1;
for( int i = 2; i <= n; i ++ ){
scanf("%d",&tmp);
str2[tmp] = i;
}
GetDp();
printf("%d\n",dp[n][n]);
}
return 0;
}