#include<bits/stdc++.h>
#define ll long long 
#define mp make_pair 
#define f(i,n) for(int i=0;i<n;i++) 
#define F first 
#define S second 
#define pb push_back 

using namespace std;

ll a[1005] , b[1005];

ll cache[1005][1005];

ll n,m;

ll dp(ll x, ll y){
	if(x==n or y==m){
		return (n+m-x-y);
	}else if(cache[x][y]!=-1)
		return cache[x][y];
	else{
		ll val = min(dp(x+1,y),dp(x,y+1)) + 1;
		if(a[x]==b[y]){
			val = min(val, dp(x+1,y+1));
		}else{
			val = min(val,dp(x+1,y+1)+1);
		}
		cache[x][y] = val;
		return val;
	}
}

void test(){
	memset(cache,-1,sizeof(cache));
	cin>>n>>m;
	f(i,n)cin>>a[i];
	f(i,m)cin>>b[i];
	cout<<dp(0,0)<<"\n";
}

int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	// cin>>tests;
	while(tests--){
		test();
	}
}
