/*** keep hungry and calm CoolGuang!  ***/
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define d(x) printf("%lld\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e18;
const ll maxn = 4e5+700;
const int mod= 1e9+7;
const int up = 1e9;
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
ll dp[1005][1005];
ll a[maxn],b[maxn];
int main(){
    read(n);read(m);
    for(int i=1;i<=n;i++) read(a[i]);
    for(int i=1;i<=m;i++) read(b[i]);
    for(int i=1;i<=n;i++)
        for(int k=1;k<=m;k++)
            dp[i][k] = INF;
    dp[0][0] = 0;
    for(int i=1;i<=n;i++) dp[i][0] = i;
    for(int k=1;k<=m;k++) dp[0][k] = k;
    for(int i=1;i<=n;i++){
        for(int k=1;k<=m;k++){
            dp[i][k] = min(dp[i-1][k]+1,dp[i][k-1]+1);
            dp[i][k] = min(dp[i][k],dp[i-1][k-1]+(a[i]!=b[k]));
        }
    }
    /*for(int i=1;i<=n;i++){
        for(int k=1;k<=m;k++){
            printf("%lld ",dp[i][k]);
        }
        printf("\n");
    }*/
    ll ans = INF;
    for(int i=0;i<=n;i++){
        for(int k=0;k<=m;k++){
            ans = min(ans,dp[i][k] + n-i + m-k);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
/***
2 4
2 3
1 1 2 3
***/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

#define rep(i, n) for(int i = 0;i < (n);i++)

template<typename T>
void chmin(T & a, const T & b) { a = min(a, b); }

vector<vector<ll>> dp;


int main() {
	const int INF = 1001001001;
	int n, m;
	cin >> n >> m;
	vector<int> a(n), b(m);
	rep(i, n) cin >> a[i];
	rep(j, m) cin >> b[j];
	dp = vector<vector<ll>>(n + 1, vector<ll>(m + 1, INF));
	dp[0][0] = 0;
	rep(i, n + 1) rep(j, m + 1) {
		if (i < n) chmin(dp[i + 1][j], dp[i][j] + 1);
		if (j < m) chmin(dp[i][j + 1], dp[i][j] + 1);
		if (i < n && j < m) {
			int co = 0;
			if (a[i] != b[j]) co = 1;
			chmin(dp[i + 1][j + 1], dp[i][j] + co);
		}
	}
	cout << dp[n][m] << endl;
	return 0;
}