#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first               
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e3+5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N)+1;

ll n, m, t, a[N], b[N], dp[N][N];

ll mem(int i, int j){
	if(i == n + 1) return m - j + 1;
	if(j == m + 1) return n - i + 1;
	if(dp[i][j] != -1) return dp[i][j];
	dp[i][j] = INFi;
	if(a[i] != b[j])
		dp[i][j] = min(dp[i][j], mem(i + 1, j + 1) + 1);
	else
		dp[i][j] = min(dp[i][j], mem(i + 1, j + 1));
	dp[i][j] = min(dp[i][j], mem(i, j + 1) + 1);
	dp[i][j] = min(dp[i][j], mem(i + 1, j) + 1);
	return dp[i][j];

}

void solve(){
	mset(dp, -1);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= m; i++) cin >> b[i];
	deb1(mem(1, 1))
}

int main(){
	GODSPEED;
	int test = 1;
	//cin >> test;
	for(int i = 1; i <= test; i++){
		solve();
	}
	#ifndef ONLINE_JUDGE
	cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
	#endif
}
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
using namespace std;
int n,m;
int dp[2000][2000];
int a[2000],b[2000];
signed main(){
	memset(dp,10,sizeof(dp));
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++)cin>>b[i];
	dp[0][0]=0;
	for(int i=1;i<=n;i++)dp[i][0]=i;
	for(int i=1;i<=m;i++)dp[0][i]=i;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			int x=1;
			if(a[i]==b[j]){
				x=0;
			}
			dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+x));
		}
	}
	cout<<dp[n][m]<<endl;
	return 0;
}