#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define INF 1e9+7
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(int i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(int i=(r)-1;i>=(l);i--)
#define pb push_back
#define ins insert
#define se second
#define fi first
#define all(x) x.begin(),x.end()
using vi=vector<int>;
using vl=vector<ll>;
using vvi= vector<vector<int>>;
using vvl= vector<vector<ll>>;
using pii= pair<int,int>;
using pll= pair<ll,ll>;
const ll mod = INF;
const int dxn[9]={-1,0,1,-1,0,1,-1,0,1};
const int dyn[9]={-1,-1,-1,0,0,0,1,1,1};
const int dxf[4]={0,1,0,-1};
const int dyf[4]={-1,0,1,0};
const int maxn = 1000;


int main(){
    int n,m;
    ll a[maxn+1],b[maxn+1];
    ll dp[maxn+1][maxn+1]={0};

    cin >> n>>m;
    rep(i,n)cin >> a[i+1];
    rep(i,m)cin >> b[i+1];

    rep(i,m+1)dp[0][i]=i;
    rep(i,n+1)dp[i][0]=i;

    repl(i,1,n+1){
        repl(j,1,m+1){
            int p=1;
            if(a[i]==b[j])p=0;
            dp[i][j]=min(min(dp[i-1][j]+1,dp[i][j-1]+1),dp[i-1][j-1]+p);
        }
    }

    cout << dp[n][m]<<endl;

    return 0;
}
#include<stdio.h>
#include<bits/stdc++.h>
#define vi vector<int>
#define int long long 
#define pf printf
#define mod 1000000007
using namespace std;
void boost(){
    ios_base::sync_with_stdio(0); 
    cin.tie(0); 
    cout.tie(0); 
}
int pow(int n,int m){
    if(m%2==0){
      int pro=pow(n,m/2);
      return ((pro)%mod*(pro)%mod)%mod;
    }else{
       return ((n)%mod*(pow(n,m-1))%mod)%mod;
    }
}
int dp[1000][1000];
int findans(vector<int>&a,vector<int>&b,int i,int j){
    if(i==a.size() || j==b.size()){
        //pf("i is %lld and j is %lld ans is %lld\n",i,j,(a.size()-i)+(b.size()-j));
        return (a.size()-i)+(b.size()-j);
    }
    if(dp[i][j]!=-1) return dp[i][j];
    if(a[i]==b[j]){
        return dp[i][j]=findans(a,b,i+1,j+1);
    }else{
        int t=1+findans(a,b,i+1,j+1);
        int l=1+findans(a,b,i+1,j);
        int r=1+findans(a,b,i,j+1);
        return dp[i][j]=min({l,r,t});
    }
}
int32_t main(){
  boost();
 int n,m;
 cin>>n>>m;
 vector<int> a,b;
 memset(dp,-1,sizeof(dp));
 for(int i=0;i<n;i++){
     int ele;
     cin>>ele;
     a.push_back(ele);
 }
 for(int i=0;i<m;i++){
     int ele;
     cin>>ele;
     b.push_back(ele);
 }
 int ans=findans(a,b,0,0);
 pf("%lld\n",ans);
return 0;
}
