#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define endl "\n"
#define test ll txtc; cin>>txtc; while(txtc--)
typedef long long int ll;
int main() {
    FIO;
    ll n,m; cin>>n>>m;
    vector<ll>a(n),b(m);
    for(auto &it:a) cin>>it;
    for(auto &it:b) cin>>it;
    vector<vector<ll>>dp(n+1,vector<ll>(m+1,mod));
    for(ll i=n;i>=0;i--){
        for(ll j=m;j>=0;j--){
            if(i==n || j==m){
                dp[i][j]=(n-i)+(m-j);
            }
            else{
                if(a[i]==b[j]){
                    dp[i][j]=dp[i+1][j+1];
                }
                else{
                    dp[i][j]=min({dp[i+1][j],dp[i][j+1],dp[i+1][j+1]})+1;
                }
            }
        }
    }
    cout<<dp[0][0]<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define irep(i,a,b) for(int i=int(a);i<(int)b;++i)
#define rrep(i,a,b) for(int i=int(a);i>=(int)b;--i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll,ll>>>
#define vpl vector<pair<ll,ll>>
#define vpi vector<pair<int,int>>
#define pb push_back
#define se second
#define fi first
#define all(v) v.begin(),v.end()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

using namespace std;

template<typename T> istream& operator>>(istream&i,v(T)&v){rep(j,v.size())i>>v[j];return i;}
template<typename T> string join(const v(T)&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T> ostream& operator<<(ostream&o,const v(T)&v){if(v.size())o<<join(v);return o;}



using ll = long long;
const ll INF = LLONG_MAX;
const double PI = acos(-1);

const ll mod = 1e9 + 7; //998244353;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}


ll modpow(ll a,ll b){
    if(b == 0){
        return 1;
    }
    if(b%2 == 0){
        ll tmp = modpow(a,b/2);
        return tmp*tmp%mod;
    }else{
        return modpow(a,b-1)*a%mod;
    }

}

template <typename T>
struct RMQ {
    const T INF = numeric_limits<T>::max();
    T bound;  //最小値のときはinf,それ以外の時0となる
    int n;
    bool flag; //最大値でtrue
    vector<T> dat;
    RMQ(int n_) : n(),dat(n_ * 4,-1){
        int x = 1;
        while(n_ > x){
            x *= 2;
        }
        n = x;
    }

    T func(T a,T b){
        if(a == -1)return b;
        if(b == -1)return a;
        return a^b;
    }

    void update(int i,T x){
        i += n - 1;
        dat[i] = x;
        while(i > 0){
            i = (i-1)/2;
            dat[i] = func(dat[i*2+1],dat[i*2+2]);
        }
    }


    T query(int a,int b){
        return query_sub(a,b,0,0,n);
    }

    T query_sub(int a,int b,int k,int l,int r){
        if(r <= a || b <= l){
            return -1;
        }else if(a <= l && r <= b){
            return dat[k];
        }else{
            T v_l = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T v_r = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return func(v_l, v_r);
        }
    }


};

ll dp[1005][1005];
int main(void)
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vl a(n),b(m);
    cin >> a;
    cin >> b;
    
    rep(i,n+1){
        rep(j,m+1){
            if(i == 0 || j == 0){
                dp[i][j] = max(i,j);
                continue;
            }
            ll tmp = min(dp[i][j-1] + 1,dp[i-1][j] + 1);
            tmp = min(tmp,dp[i-1][j-1] + (a[i-1] == b[j-1] ? 0 : 1));
            dp[i][j] = tmp;
        }
    }

    cout << dp[n][m] << endl;
    
    return 0;
    
}
