#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define P pair
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000;
constexpr ll llINF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
ll gcd(ll a,ll b){
    if(b==0)return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
ll modpow(ll a,ll b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    ll memo=modpow(a,b/2);
    return memo*memo%mod;
}
vector<ll>kaijo,invkaijo;
void init_fact(int n){
    kaijo.resize(n+1);
    invkaijo.resize(n+1);
    kaijo[0]=1;
    for(ll i=1;i<=n;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
    rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0)return 0;
    return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod;
}
ll inv(ll x){
    x=modpow(x,mod-2);
    return x;
}
void solve(){

}
int dp[1010][1010];
int main(){
    int n,m,a[1001],b[1001];
    cin>>n>>m;
    rep(i,n)cin>>a[i];
    rep(i,m)cin>>b[i];
    rep(i,n+1)rep(j,m+1)dp[i][j]=INF;
    dp[0][0]=0;
    rep(i,n+1){
        rep(j,m+1){
            if(j<m){
                chmin(dp[i][j+1],dp[i][j]+1);
            }
            if(i<n){
                chmin(dp[i+1][j],dp[i][j]+1);
            }
            if(i<n&&j<m){
                if(a[i]==b[j])chmin(dp[i+1][j+1],dp[i][j]);
                else chmin(dp[i+1][j+1],dp[i][j]+1);
            }
        }
    }
    cout<<dp[n][m]<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define mod 1000000007
#define M 1000000007
#define pb push_back
#define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mx 1000005
#define fiint(a,b) memset(a,b,sizeof(a))
#define bitcount __builtin_popcount
#define fori(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cout<<x<<endl;
#define cases(t) int t; cin>>t; while(t--)
#define inf1 INT_MAX
#define all(a) a.begin(),a.end()
#define vec vector<int>
#define pii pair<int,int>
#define plii pair<int,int>
#define pint pair<int,int>
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mp make_pair
 
#define sz(x) (int)x.size()
#define PI    3.14159265359

//const long long INF=1e18;

bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }

 
int factorial[10000001]={0};
 
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}


int powerFunction(int x,int y){
    int res = 1;
     int p = mod;
    x = x;
    while (y > 0){
        if (y & 1)
            res = (res*x)%p ;
        y = y>>1;
        x = (x*x)%p ;
    }
    return res;
}

void factorialFunction(int maxLimit){
    factorial[0]=1;
    for(int i=1 ; i <= maxLimit ; i++)
    factorial[i]=(factorial[i-1]*i)%MOD;
    return;
}
 
int nCr(int n, int r)
{
    if(r < 0 || r > n){
        return 0;
    }
    int temp = factorial[n];
    temp *= (powerFunction(factorial[r], MOD-2)%MOD);
    temp %= MOD;
    temp *= (powerFunction(factorial[n-r], MOD-2)%MOD);
    temp %= MOD;
    return temp;
}

int n,m;
vec a(2000)  , b(2000) ;
int dp[2000][2000];
int find(int i , int j)
{
    if(i >= n || j >= m)
        return (n - i) + (m - j) ;
    
    if(dp[i][j] != -1)
        return dp[i][j];
    
   
        int x = find(i+1 , j) + 1;
        int y = find(i , j+1) + 1 ;
        int z = find(i+1 , j+1) + (int)(a[i] != b[j]);
        //int c = find(i + 1 , j + 1 )  +2;
        dp[i][j] = min( z ,  min(x,y ));
        //cout<<dp[i][j] <<" "<<i <<" "<<j<<endl;
  
    return dp[i][j];
}

signed main()
{
    cin>>n>>m;
    for(int i = 0  ; i < n ;i++) cin>>a[i];
    for(int j = 0 ; j < m ;j++) cin>>b[j];
    for(int i = 0 ; i < 2000 ; i++)
        for(int j = 0 ; j < 2000 ; j++)
                dp[i][j] = -1;
    int  mi = find(0,0);
   // cout<<mi<<endl;
    cout << mi <<endl;
}
    


