#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   Y            imag()
#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
#define    dp(a,b)          (((conj(a))*(b)).real())
#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     point                    complex<long double>
#define     PI                       acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-15);
using namespace std;
int dcmp(double a,double b){   return fabs(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
const ll N=1000+9,mod=998244353,sh=1000000,inf=1000000;
int n,m,a[N],b[N],mem[N][N][6];
int solve(int id1,int id2,int fl){

    if(id1>=n&&id2>=m){
        if(fl==1){
            return 0;
        }
        return inf;
    }
    if(id1>=n){
        if(fl==1) return (m-id2);
        return inf;
    }
    if(id2>=m){
        if(fl==1) return (n-id1);
        return inf;
    }

    int &ret=mem[id1][id2][fl];
    if(ret!=-1) return ret;
    int ans=inf;

    if(fl==2){
    if(id1<n&&id2<m)
    ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1));
    if(id1<n&&id2+1<m)
    ans=min(ans,1+solve(id1,id2+1,3));
    if(id2<m&&id1+1<n)
    ans=min(ans,1+solve(id1+1,id2,4));
    }
    else if(fl==1){
        if(id1<n&&id2<m)
        ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1));
        if(id1<n&&id2+1<m)
        ans=min(ans,1+solve(id1,id2+1,3));
        if(id2<m&&id1+1<n)
        ans=min(ans,1+solve(id1+1,id2,4));
    }
    else if(fl==3){
        if(id1<n&&id2<m)
        ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1));
        if(id1<n&&id2+1<m)
        ans=min(ans,1+solve(id1,id2+1,3));
    }
    else if(fl==4){
        if(id1<n&&id2<m)
        ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1));
        if(id2<m&&id1+1<n)
        ans=min(ans,1+solve(id1+1,id2,4));

    }
 return ret=ans;

}
int main(){
    GO();

    int T=1;
    //cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=0;i<n;i++) cin>>a[i];
        for(int i=0;i<m;i++) cin>>b[i];
        clr(mem,-1);

        cout<<solve(0,0,2)<<"\n";

    }

}












#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include<bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define pii                 pair<int,int>
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define pii                 pair<int,int>
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)           for(int i=0;i<n;i++)
#define for1(i,n)           for(int i=1;i<=n;i++)
#define forrev(i,n)         for(int i=n-1; i>=0; i--)
#define forab(i,a,b)        for(int i=a;i<=b;i++)
#define forba(i,b,a)        for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define N                   300005
#define inf                 100000008
#define mod                 1000000007

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/










ll a[1003],b[1003];
ll dp[1003][1003];

int main()
{
    ll n,m,i,j;
    cin>>n>>m;
    for1(i,n)cin>>a[i];
    for1(i,m)cin>>b[i];
    for0(i,n+1)for0(j,m+1)dp[i][j] = max(i,j);

    for1(i,n)
    {
        for1(j,m)
        {
            if(a[i]==b[j])
            {
                dp[i][j] = min3(dp[i][j],dp[i-1][j-1],min(dp[i-1][j]+1,dp[i][j-1]+1));
            }
            else
            {
                dp[i][j] = min3(dp[i][j],dp[i-1][j-1]+1,min(dp[i-1][j]+1,dp[i][j-1]+1));
            }
        }
    }
    cout<<dp[n][m]<<endl;
	return 0;
}


















