#include<bits/stdc++.h>
using namespace std;

using i64 = long long;
using u64 = unsigned long long;

struct debug {
    ~debug(){cerr << endl;}
    template<class c> debug& operator<<(c x) {cerr << x; return *this;}
    template<class c, class d>
    debug& operator<<(pair<c, d> x) {*this << "(" << x.first << ", " << x.second << ")"; 
        return *this;}
    template<class c> debug& operator<<(vector<c> x) { *this << "[";
        int f = 0; for(auto it : x) { *this << (f?", ":""); *this << it; f = 1;}
        *this << "]"; return *this;}
};

#define dbg(x) "[" << #x << ": " << x << "]  "
#define Wa() cerr << "[LINE: " << __LINE__ << "] -> "; debug() << 
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL)


int dp[1000+3][1000+3];

vector<int> a, b;

int f(int i, int j)
{
    if(i == 0 || j == 0) {
        return abs(i-j);
    }
    int& ret = dp[i][j];
    if(ret != -1) return ret;
   
    ret = min({f(i-1, j) + 1, f(i, j-1) +1, f(i-1, j-1) + (a[i-1] != b[j-1])});
    return ret;
}

int main()
{
    int n, m;
    cin >> n >> m;

    a = vector<int> (n);
    b = vector<int> (m);
    for(int& i : a) cin >> i;
    for(int& i : b) cin >> i;

    memset(dp, -1, sizeof dp);

    cout << f(n, m) << '\n';
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


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<ll>> dp(n+10, vector<ll>(m+10, LINF));
    dp[0][0] = 0;
    rep(i, n+1){
        rep(j, m+1){
            chmin(dp[i+1][j], dp[i][j]+1);
            chmin(dp[i][j+1], dp[i][j]+1);
            if(A[i]==B[j]){
                chmin(dp[i+1][j+1], dp[i][j]);
            }else{
                chmin(dp[i+1][j+1], dp[i][j]+1);
            }
        }
    }
    cout << dp[n][m] << ln;
}   