#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
int a[10001];
int b[10001];
int n,m;
int lengthOfLCIS() {
int ans=0;
int dp[1050][1050]={0};
int max_dp=0;
for (int i=0;i<n;++i) {
for (int j=0;j<m;++j) {
dp[i+1][j+1]=dp[i][j+1];
if (a[i]==b[j]) {
max_dp=0;
for (int k=0;k<j;++k){
if (b[j]>b[k]&&max_dp<=dp[i][k+1]){
max_dp=dp[i][k+1];
}
}
dp[i+1][j+1]=max_dp+1;
}
ans=ans>dp[i+1][j+1]?ans:dp[i+1][j+1];
}
}
return ans;
}
int main(){
int anss;
freopen("lcis.in","r",stdin);
freopen("lcis.out","w",stdout);
cin>>n;
for (int i = 0 ; i < n ; ++i) {
cin>>a[i];
}
cin>>m;
for (int j = 0 ; j < m ; ++j) {
cin>>b[j];
}
anss=lengthOfLCIS();
cout<<anss;
}