#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();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcgCiNkZWZpbmUgbXAgbWFrZV9wYWlyIAojZGVmaW5lIGYoaSxuKSBmb3IoaW50IGk9MDtpPG47aSsrKSAKI2RlZmluZSBGIGZpcnN0IAojZGVmaW5lIFMgc2Vjb25kIAojZGVmaW5lIHBiIHB1c2hfYmFjayAKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpsbCBhWzEwMDVdICwgYlsxMDA1XTsKCmxsIGNhY2hlWzEwMDVdWzEwMDVdOwoKbGwgbixtOwoKbGwgZHAobGwgeCwgbGwgeSl7CglpZih4PT1uIG9yIHk9PW0pewoJCXJldHVybiAobittLXgteSk7Cgl9ZWxzZSBpZihjYWNoZVt4XVt5XSE9LTEpCgkJcmV0dXJuIGNhY2hlW3hdW3ldOwoJZWxzZXsKCQlsbCB2YWwgPSBtaW4oZHAoeCsxLHkpLGRwKHgseSsxKSkgKyAxOwoJCWlmKGFbeF09PWJbeV0pewoJCQl2YWwgPSBtaW4odmFsLCBkcCh4KzEseSsxKSk7CgkJfWVsc2V7CgkJCXZhb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