#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
int n,a[1001],b[1001],f[1001][1001];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freeopen(".in","r",stdin);
//freeopen(".out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(a[i]==b[j]) f[i][j]=f[i-1][j-1]+1;
else f[i][j]=max(f[i-1][j],f[i][j-1]);
}
}
cout<<f[n][n];
return 0;
}