//@sakshjha
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld   long double
#define JALDI ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define pi 3.14159265358979

const int mod = 1e9 +7;
const int N = 1e6 +5 ;
int a[1005],b[1005];
int dp[1005][1005];
int solve(int x, int y)
{
    if(x<0)
        return y+1;
    if(y<0)
        return x+1;
    if(dp[x][y]==-1)
    {
        dp[x][y]=min(solve(x-1,y),solve(x,y-1))+1;
        int tmp=0;
        if(a[x]!=b[y])
            tmp=1;
        dp[x][y]=min(dp[x][y],solve(x-1,y-1)+tmp);
    }
    return dp[x][y];

}
int32_t main()
{
    JALDI
    int i,j,tc=1;
    //cin>>tc;
    while(tc--)
    {
        int n,m; cin>>n>>m;
        for(i=0; i<n; i++)
            cin>>a[i];
        for(i=0; i<m; i++)
            cin>>b[i];
        memset(dp,-1,sizeof(dp));
        cout<<solve(n-1,m-1);
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         cout<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define endl            "\n"
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());


void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new  ( std::size_t count ) {
    sz += count;
    assert_tle(sz <= 200 * 1024 * 1024);
    return myHeap + sz - count;
}
 
void operator delete (void *ptr) { }
 
 
void fastIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}



void solve()
{
    int n,m;
    cin>>n>>m;

    int ar[n],br[m];
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    for(int i=0;i<m;i++)
    {
        cin>>br[i];
    }

    int memo[n+1][m+1]={0};

    for(int i=1;i<=n;i++)
    {
        memo[i][0]=i;
    }
    for(int j=1;j<=m;j++)
    {
        memo[0][j]=j;
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int mn=1+min(memo[i-1][j],memo[i][j-1]);
            memo[i][j]=mn;
            if(ar[i-1]==br[j-1])
            {
                memo[i][j]=min(memo[i][j],memo[i-1][j-1]);
            }
            else
            {
                memo[i][j]=min(memo[i][j],1+memo[i-1][j-1]);
            }

        }
    }

    cout<<memo[n][m];
}
 
int32_t main()
{
    fastIO();
    //w(t)
    {
        solve();
        cout<<endl;
    }
    return 0;
}