#include <cstdio>
int a[1005];
int b[1005];
int f[1005][1005];
int vis[1005][1005];

int max(int a,int b){ return a>b?a:b; }

void solve(int i,int j){
    vis[i][j] = 1;
    if(i==0 || j==0) f[i][j] = max(i,j);
    else{
        if(!vis[i-1][j]) solve(i-1,j);
        if(!vis[i][j-1]) solve(i,j-1);
        if(!vis[i-1][j-1]) solve(i-1,j-1);
        f[i][j] = 1e8+7;
        if(f[i][j] > f[i-1][j]+1) f[i][j] = f[i-1][j]+1;
        //if(i==2 && j==1) printf("1: %d\n",f[i][j]);
        if(f[i][j] > f[i][j-1]+1) f[i][j] = f[i][j-1]+1;
        //if(i==2 && j==1) printf("2: %d\n",f[i][j]);
        int offer = f[i-1][j-1] + (a[i]!=b[j]);
        //if(i==2 && j==1) printf("offer = %d\n",offer);
        if(f[i][j] > offer) f[i][j] = offer;
        //if(i==2 && j==1) printf("3: %d\n",f[i][j]);
    }
    //printf("f[%d][%d] = %d\n",i,j,f[i][j]);
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
    for(int i = 1; i <= m; i++) scanf("%d",&b[i]);
    solve(n,m);
    printf("%d\n",f[n][m]);
    return 0;
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
#include <bits/stdc++.h>
#define overload3(_1, _2, _3, name, ...) name
#define rep1(i, n) for(i64 i = 0LL; i < (n); ++i)
#define rep2(i, a, b) for(i64 i = (a); i < (b); ++i)
#define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)
#define all(v) v.begin(), v.end()

using namespace std;
using i64 = long long int;


i64 solve(int N, int M, const vector<i64> & A, const vector<i64> & B) {
    // TODO: edit here
    vector<vector<i64>> dp(N + 1, vector<i64>(M + 1));
    rep(i, N + 1) dp[i][0] = i;
    rep(i, M + 1) dp[0][i] = i;
    rep(i, N){
        rep(j, M){
            dp[i + 1][j + 1] = min({dp[i][j + 1] + 1, dp[i + 1][j] + 1, dp[i][j] + (A[i] == B[j] ? 0LL : 1LL)});
        }
    }
    return dp[N][M];
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    constexpr char endl = '\n';
    int N, M;
    cin >> N;
    vector<i64> A(N);
    cin >> M;
    vector<i64> B(M);
    rep (i, N) {
        cin >> A[i];
    }
    rep (i, M) {
        cin >> B[i];
    }
    auto ans = solve(N, M, A, B);
    cout << ans << endl;
    return 0;
}
