#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<cstdint>
#include<algorithm>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = size_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i64 i = 0; i < (i64)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;
void solve(){
}
int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    u64 n,m;
    cin >> n >> m;
    vec<u64> a(n),b(m);
    rep(i,n)cin >> a[i];
    rep(i,m)cin >> b[i];
    vec<vec<u64>> dp(n + 1,vec<u64> (m + 1,1e18));
    dp[0][0] = 0;
    rep(i,n + 1){
        rep(j,m + 1){
            if(i && 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);
            }
            if(i)dp[i][j] = min(dp[i][j],dp[i - 1][j] + 1);
            if(j)dp[i][j] = min(dp[i][j],dp[i][j - 1] + 1);
        }
    }
    cout << dp.back().back() << endl;
}
#include<bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    ios::sync_with_stdio(false); 
    cin.tie(0); 

    int n, m;
    cin >> n >> m;

    vector<int> a(n);
    vector<int> b(m);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < m; i++) cin >> b[i];

    // either remove this character from b, or remove it from a

   	vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1 << 30));

   	dp[0][0] = 0;
   	for(int i = 0; i <= n; i++) {
   		for(int j = 0; j <= m; j++) {
   			int here = dp[i][j];
   			if(here == 1 << 30) continue;
   			
   			// so given that we are considering position i, j in both strings
   			// we can either either remove from i => + 1
   			// remove from j => + 1
   			// we dont remove from either, then + whether or not a_i != b_j

   			if(i + 1 <= n)
   				dp[i + 1][j] = min(1 + here, dp[i + 1][j]);
   			if(j + 1 <= m)
   				dp[i][j + 1] = min(1 + here, dp[i][j + 1]);
   			if(i + 1 <= n && j + 1 <= m)
   				dp[i + 1][j + 1] = min((a[i] == b[j] ? 0 : 1) + here, dp[i + 1][j + 1]);
   		}
   	}

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