#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
void solve()
{
    int n,m;cin>>n>>m;
    int a[n],b[m],dp[n+1][m+1];
    for(auto &i:a)cin>>i;
    for(auto &i:b)cin>>i;
    REP(i,n+1)dp[i][0]=i;
    REP(i,m+1)dp[0][i]=i;
    rep(i,1,n+1)rep(j,1,m+1)dp[i][j]=min({dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(a[i-1]==b[j-1]?0:1)});
    cout<<dp[n][m]<<endl;
}

int main()
{
    cin.tie(0);
	ios::sync_with_stdio(false);
    solve();
    return 0;
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long ll;

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(N), B(M);
    rep(i, N) cin >> A[i];
    rep(i, M) cin >> B[i];
    vector<vector<int>> dp(N, vector<int>(M, 0));
    rep(i, N) {
        rep(j, M) {
            if (i == 0 && j == 0) {
                if (A[i] == B[j]) dp[i][j] = 0;
                else dp[i][j] = 1;
                continue;
            }
            int x = 1e9, y = 1e9, z = 1e9;
            if (j > 0) {
                x = dp[i][j - 1] + 1;
                if (A[i] != B[j - 1]) x--;
                if (A[i] != B[j]) x++;
            }
            if (i > 0) {
                y = dp[i - 1][j] + 1;
                if (A[i - 1] != B[j]) y--;
                if (A[i] != B[j]) y++;
            }
            if (i > 0 && j > 0) {
                z = dp[i - 1][j - 1];
                if (A[i] != B[j]) z++;
            }
            dp[i][j] = min({x, y, z});
        }
    }
    /*
    rep(i, N) {
        rep(j, M) {
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }
    */
    int ans = 1e9;
    rep(i, N) {
        rep(j, M) {
            ans = min(ans, dp[i][j] + N - 1 - i + M - 1 - j);
        }
    }
    cout << ans << "\n";
}