#include "bits/stdc++.h"

#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
#define vec vector<int>
#define fo(i, n) for (int i = 0; i < n; i++)
#define foo(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define ll long long
#define mod 1000000007
//#define mod 998244353
#define so(v) sort(v.begin(), v.end())
#define fi first
#define se second

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
//__builtin_popcount
//vector<vector<int>>
//     dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int power(int x, unsigned int y, int p)
{
    int res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
int mI(int a, int m)
{
    return power(a, m - 2, m);
}

#define double long double

int n,m;
vec v1(1001), v2(1001);
int dp[1001][1001];
int fun(int i,int j){
    //debug(i, j);
    if(i==n){
        return m-j;
    }
    if(j==m){
        return n - i;
    }
    
    if(dp[i][j]!=-1)
        return dp[i][j];
    int g = 0;
    if(v1[i]!=v2[j])
        g = 1;
    return dp[i][j]=min({fun(i + 1, j + 1) + g, fun(i, j + 1)+1,fun(i+1,j)+1});
}




void solve(){
    //int n, m;
    
    cin >> n>>m;
    fo(i,n) { 
        cin >> v1[i]; 
    }
    fo(i,m){
        cin >> v2[i];
    }
    // if(n>m){
    //     swap(n, m);
    //     swap(v1, v2);
    // }
    memset(dp, -1, sizeof(dp));
    // int val = fun(0, 0);
    // //val = min({val, n, m});
    // val = max(m,n) - val;
    cout << fun(0,0) << "\n";

}


int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    srand(time(0));
    int t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
}
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long
int m=1e9+7;
int main()
{
	int n,m,temp1,temp2;
	cin>>n>>m;
	int a[n];
	int b[m];
	int c[m+1];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++){cin>>b[i];c[i]=i;}
	c[m]=m;
	for(int i=1;i<=n;i++)
	{
		c[0]=i;
		temp2=i-1;
		for(int j=1;j<=m;j++)
		{
			temp1=c[j];
			if(a[i-1]==b[j-1])c[j]=temp2;
			else
			{
				c[j]=min(1+temp2,min(c[j]+1,c[j-1]+1));
			}
			temp2=temp1;
		}
	}
	cout<<c[m];
	return 0;
}