#include <bits/stdc++.h>
#define pb push_back
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define INF 3000000000000001 // 3e15
#define ll_max 9000000000000000000
#define PI 3.14159265358979323846L
#define fill(a, n, x) for(int i = 0; i < n; i++) a[i] = x;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll mod = 1000000007;
const ll N = 200011;
const ll M = log2(N) + 1;

struct point
{
    ll x, y;
};

template<typename T>
T fast_exp(T x,T n)
{
    if(n == 0) return 1;
    T ans = 1;
    if( n % 2 == 0)
    {
         ans = fast_exp(x, n / 2);
         ans = ans * ans % mod;
    }
    else ans = fast_exp(x, n - 1) * x % mod;
    return ans;
}

ll fact[N], infac[N];
void work()
{
    fact[0] = 1;
    infac[0] = 1;
    for(ll i = 1; i < N; i++)
    {
        fact[i] = fact[i - 1] * i % mod;
        infac[i] = fast_exp(fact[i], mod - 2);
    }
}

ll ncr(ll n, ll r)
{
    return fact[n] * infac[n - r] % mod * infac[r] % mod;
}

ll depth[N], anc[N][M];
set<ll> val[N][M];
void dfs(vector<int> tree[], vector<ll> lab[], int x, int p, int d = 0)
{
    depth[x] = d;
    if(lab[p].size() > 0)
    {
        for(auto i:lab[p]) val[x][0].insert(i);
    }
    if(lab[x].size() > 0)
    {
        for(auto i:lab[x]) val[x][0].insert(i);
    }
    anc[x][0] = p;
    for(int i = 1; i < M; i++)
    {
        anc[x][i] = anc[anc[x][i - 1]][i - 1];
        set<ll> &v1 = val[x][i - 1];
        set<ll> &v2 = val[anc[x][i - 1]][i - 1];
        for(auto j:v1)  val[x][i].insert(j);
        for(auto j:v2)  val[x][i].insert(j);
        while(val[x][i].size() > 10)
            val[x][i].erase(val[x][i].begin());
    }
    for(auto i:tree[x])
    {
        if(i != p)
            dfs(tree, lab, i, x, d + 1);
    }
}

set<ll> LCA(ll l, ll r, ll g)
{
    set<ll> s;
    if(depth[l] != depth[r])
    {
        if(depth[l] > depth[r])
        swap(l, r);
        ll x = depth[r] - depth[l];
        for(int i = M - 1; i > -1; i--)
        {
            if(x & (1ll << i)) {
                for(auto j:val[r][i]) s.insert(j);
                r = anc[r][i];
            }
        }
    }
    if(l == r) return s;
    for(int i = M - 1; i > -1; i--)
    {
        if(anc[l][i] != anc[r][i])
        {
            for(auto j:val[r][i]) s.insert(j);
            for(auto j:val[l][i]) s.insert(j);
            l = anc[l][i];
            r = anc[r][i];
        }
    }
    for(auto j:val[l][0]) s.insert(j);
    while(s.size() > g)
            s.erase(s.begin());
    return s;
}

bool solve(vector<int> tree[], ll x, ll p, ll tar, vector<ll> lab[], set<ll> &s)
{
    bool ans = 0;
    if(x == tar)
        ans = 1;
    for(auto i:tree[x])
    {
        if(i != p)
        {
            ans = ans | solve(tree, i, x, tar, lab, s);
        }
    }
    if(ans)
    {
        for(auto i:lab[x]) s.insert(i);
    }
    return ans;
}

int main()
{
    fast;
    int _ = 1;
    //cin >> _;
    int test = 0;
    while(_--)
    {
        test++;
        ll n, m, x, y, ans = 0, k;
        cin >> n >> m;
        ll a[n + 1], b[m + 1];
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= m; i++) cin >> b[i];
        ll dp[n + 1][m + 1];
        for(int i = 0; i <= n; i++)
        {
            for(int j = 0; j <= m; j++)
                dp[i][j] = i + j - min(i, j);
        }
        dp[0][0] = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1];
                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] << "\n";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());
const ll mod=1e9+7;
 
const int N=1010;
ll dp[N][N], n, m;
vector<ll> a,b;

ll go(ll l, ll r){
    if(l==n)
        return m-r;
    if(r==m)
        return n-l;
    if(dp[l][r]!=-1)
        return dp[l][r];
    ll ans=mod;
    if(a[l]==b[r])
        ans=min(ans,go(l+1,r+1));
    else
        ans=min(ans,1+go(l+1,r+1));
    ans=min(ans,1+go(l+1,r));
    ans=min(ans,1+go(l,r+1));
    //cout<<l<<" "<<r<<" "<<ans<<"\n";
    return dp[l][r]=ans;
}

int main() {
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=clock();
 
    cin>>n>>m;
    a.assign(n,0);
    b.assign(m,0);
    for(ll i=0;i<n;i++)
        cin>>a[i];
    for(ll i=0;i<m;i++)
        cin>>b[i];
    memset(dp,-1,sizeof(dp));
    cout<<go(0,0);
 
 
    cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
    T = clock();
    return 0;
}