#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 3000 + 5;
int n, m, ans;//第一个长度 第二个长度 答案
int a[MAXN], b[MAXN];//输入数组
int f[MAXN][MAXN];//动态规划数组
int main()
{
freopen("lcis.in","r",stdin);
freopen("lcis.out","w",stdout);
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; ++i) scanf("%d", &b[i]);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
f[i][j] = f[i - 1][j];//不选的话长度是上一个的长度
if (a[i] == b[j])//相同
{
int mx = 1;//初始长度
for (int k = 1; k < j; ++k)//1~j-1
{
if (a[i] > b[k])//是上升序列
{
mx = max(mx, f[i - 1][k] + 1);//长度找最长的
}
}
f[i][j] = max(f[i][j], mx);//保存最长长度
}
ans = max(ans, f[i][j]);//保存答案
}
}
printf("%d\n", ans);
return 0;
}