#include<fstream>
using namespace std;
ifstream cin("crossa.in");
ofstream cout("crossa.out");
int n,m,a[300],b[300],f[300][300];
void solve()
{
int i,j,k,x,y;
bool havea,haveb;
cin>>n>>m;
for (i=1;i<=n;i++)
cin>>a[i];
for (i=1;i<=m;i++)
cin>>b[i];
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
f[i][j]=f[i-1][j];
if (f[i][j]<f[i][j-1])
f[i][j]=f[i][j-1];
if (a[i]!=b[j])
{
havea=0;
haveb=0;
for (k=j-1;k>=1;k--)
if (b[k]==a[i])
{
y=k;
havea=1;
break;
}
for (k=i-1;k>=1;k--)
if (a[k]==b[j])
{
x=k;
haveb=1;
break;
}
if ((havea)&&(haveb)&&(f[i][j]<f[x-1][y-1]+1))
f[i][j]=f[x-1][y-1]+1;
}
}
cout<<f[n][m]*2<<endl;
}
int main(int argc, char *argv[])
{
solve();
return 0;
}