#include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;

inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9')
	{
		if(s=='-') f=-1;
		s=getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=x*10+s-48;
		s=getchar();
	}
	return f*x;
}

const int N=2005;
int a[N],b[N],dp[N][N];

int main()
{
	int n=read(),m=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=m;i++) b[i]=read();
	memset(dp,0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1;i<=n;i++) dp[i][0]=i;
  	for(int i=1;i<=m;i++) dp[0][i]=i;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1];
			dp[i][j]=min(dp[i][j],1+min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]));
		}
	printf("%d\n",dp[n][m]);
	return 0;
} 
//Bismillahir Rahmanir Raheem
#ifdef ONLINE_JUDGE
    #include<bits/stdc++.h>
#endif
#include<iostream>
#include<numeric>
#include<tuple>
#include<iomanip>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<math.h>
#include<vector>
#include<climits>
#include<algorithm>
#include<string>
#include<sstream>
#include<string.h>
#include<deque>
#include<set>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(cnt, x) memset((cnt), (x), sizeof(cnt))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int,int> > vii;
typedef pair<int, int> pii;
typedef vector<bool> vbl;
typedef vector<pair<ll,ll> > vll;
typedef pair<ll, ll> pll;
#define PQ priority_queue
#define ff first
#define ss second
#define MP make_pair
#define MT make_tuple
#define mxN (int)2e5 + 2
#define sz(n) (int)(n).size()
#define fasterIO ios::sync_with_stdio(0); cin.tie(0);
#define forc(cn) for(auto &x : (cn))
#define findc(cn, x) ((cn).find(x) != (cn).end())

#define PI 2*acos(0)
#define forc(cn) for(auto &x : (cn))
#define MAXN 300010
#define torad ((2*acos(0))/180.0)
const int inf = INT_MAX;
int n, m;
vi a, b;
int dp[1010][1010];
int f(int i, int j) {
    if(i == n || j == m) {
        return max(n - i, m - j);
    }
    int &ret = dp[i][j];
    if(ret != -1) return ret;
    ret = inf;
    if(a[i] != b[j]) {
        ret = min(ret, f(i + 1, j) + 1);
        ret = min(ret, f(i, j+1) + 1);
    }
    ret = min(ret, f(i+1, j+1) + (a[i] != b[j]));
    return ret;

}
void solve()
{
    clr(dp, -1);
    cin >> n >> m;
    a = vi(n);
    b = vi(m);
    forc(a) cin >> x;
    forc(b) cin >> x;
    cout << f(0, 0) << endl;
}
int main()
{
    //fasterIO
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
