#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int a[201],b[201],i,j,k,h,n,m;
int f[201][201];
int main()
{
freopen("crossa.in","r",stdin);
freopen("crossa.out","w",stdout);
scanf("%d%d\n",&n,&m);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (i=1;i<=n;i++) scanf("%d",&b[i]);
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
f[i][j]=max(f[i-1][j],f[i][j-1]);
if (a[i]!=b[j])
{
for (k=1;k<=i;k++)
for (h=1;h<=j-1;h++)
if (a[i]==b[h] && b[j]==a[k] && f[k][h]+2>f[i][j])
f[i][j]=f[k][h]+2;
}
}
printf("%d\n",f[n][m]);
return 0;
}