#include<bits/stdc++.h>

using namespace std;

/**templates**/
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;

#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define sull(x) scanf("%llu",&x)
#define sf(x) scanf("%lf",&x)
#define ss(x) scanf(" %s",x)
#define sc(x) scanf(" %c",&x)
#define pi acos(-1.0)
#define pb push_back
#define aa first
#define bb second
#define sz(x) (ll)(x).size()
#define cas printf("Case %lld: ",++t)
#define casline printf("Case %lld:\n",++t)
//#define cas cout<<"Case "<<++t<<": "
//#define casline cout<<"Case "<<++t<<":"<<endl;
#define distance(x1,y1,x2,y2)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
#define endl "\n"
#define bug cout<<"bugggggggggggg "<<endl;
#define fast ios_base::sync_with_stdio(0);cin.tie(nullptr); ///no printf/scanf
#define clean fflush(stdout)

const ll mod = 1000000007;
const ll inf = 1LL<<62;
const ll mx = 100005;
const double eps = 1e-10;

ll dx[10]={1,0,-1,0};
ll dy[10]={0,-1,0,1};
///ll dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; /// 8 Direction
///ll dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; /// Knight Direction

ll power(ll a,ll b)
{
    if(b==0)return 1;
    ll x=power(a,b/2);
    x=x*x;
    if(b%2)x=x*a;
    return x;
}

ll bigmod(ll a,ll b)
{
    if(b==0)return 1;
    ll x=bigmod(a,b/2)%mod;
    x=(x*x)%mod;
    if(b%2)x=(x*a)%mod;
    return x;
}

ll Set(ll N,ll pos){return N=N | (1LL<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1LL<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1LL<<pos));}

/**
        ...ENDING OF TEMPLATE...
*/

ll arr[1005],brr[1005];
ll dp[1005][1005],n,m;

ll solve(ll i,ll j)
{
//    if(i>n and j>m)
//        return 0;
    if(i>n)
        return (m-j+1);
    if(j>m)
        return (n-i+1);
    ll &ret=dp[i][j];
    if(ret!=-1)
        return ret;
    ret=(arr[i]!=brr[j])+solve(i+1,j+1);
    ret=min(ret,solve(i+1,j)+1);
    ret=min(ret,solve(i,j+1)+1);
    return ret;
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
//    fast; ///no printf/scanf
    /** Use "long double"(%Lf) instead of double for precision safety*/
    ll tst,a,b,c,k,res=0,ans=0,t=0;
    sl(n),sl(m);
    for(ll i=1;i<=n;i++)
    {
        sl(arr[i]);
    }
    for(ll i=1;i<=m;i++)
    {
        sl(brr[i]);
    }
    memset(dp,-1,sizeof(dp));
    res=solve(1,1);
    cout<<res<<endl;



    return 0;
}

// https://atcoder.jp/contests/abc185/tasks/abc185_e
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,m;
    cin>>n>>m;
    int a[n+1],b[m+1];
    for (int i=1;i<=n;++i) {
        cin>>a[i];
    }
    for (int i=1;i<=m;++i) {
        cin>>b[i];
    }
    int f[n+1][m+1];
    memset(f,0x3f,sizeof f);
    f[0][0] = 0;
    for (int i=0;i<=n;++i) {
        for (int j=0;j<=m;++j) {
            if (i == 0 || j == 0) {
                // f[0][1] = 1, f[3][0] = 3
                f[i][j] = max(i,j);
                continue;
            }
            // 删除 i
            f[i][j] = min(f[i][j], f[i-1][j]+1);
            // 删除 j
            f[i][j] = min(f[i][j], f[i][j-1]+1);
            // 保留 i 和 j
            f[i][j] = min(f[i][j], f[i-1][j-1] + ((a[i] != b[j]) ? 1 : 0));
        }
    }
    cout<<f[n][m]<<endl;
    return 0;
}
