#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<(n); ++i)
#define rep1(i,n) for(ll i=1; i<=(n); ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 62) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }

int a[1005], b[1005];
int dp[1005][1005];

int main() {
    fastio;
    ll N,M;
    cin >> N >> M;
    rep1(i,N) cin >> a[i];
    rep1(i,M) cin >> b[i];
    rep(i,N+3) dp[i][0] = i;
    rep(i,M+3) dp[0][i] = i;
    rep1(i,N) rep1(j,M) {
        int t = dp[i-1][j-1] + (a[i] != b[j]);
        updmin(t, dp[i-1][j] + 1);
        updmin(t, dp[i][j-1] + 1);
        dp[i][j] = t;
    }
    cout << dp[N][M] << newl;
}
#include <iostream>

using namespace std;
int a[1010],b[1010],dp[1010][1010];
int main(){
    int i,j,n,m; cin >> n >> m;
    for(i=0;i<n;i++) cin >> a[i];
    for(j=0;j<m;j++) cin >> b[j];
    for(i=0;i<=n;i++){
        for(j=0;j<=m;j++) dp[i][j] = 100000000;
    }
    for(i=0;i<=n;i++) dp[i][0] = i;
    for(j=0;j<=m;j++) dp[0][j] = j;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(a[i - 1]==b[j - 1]){
                dp[i][j] = min(dp[i][j],dp[i - 1][j - 1]);
            }else{
                dp[i][j] = min(dp[i][j],dp[i - 1][j - 1] + 1);
            }
            dp[i][j] = min(dp[i][j],dp[i - 1][j] + 1);
            dp[i][j] = min(dp[i][j],dp[i][j - 1] + 1);
        }
    }
    cout << dp[n][m] << endl;
}