#include<bits/stdc++.h> 
using namespace std;

#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair 
#define pb push_back
#define ff first
#define ss second
#define fast  ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007 
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){
  if(p==0) return 1;
  else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;} 
  else return (x*(fast_expo(x,p-1))%MOD)%MOD;}

ll dp[1005][1005], n, m, arr1[1005], arr2[1005];
vector<ll> AdjL[1005];

int main(){
  fast
  #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
  #endif
  
  ll t_c; t_c = 1;
  // cin >> t_c;

  for(ll t_i = 0; t_i < t_c; ++t_i){
    ll n, m; cin >> n >> m;
    ll a[n + 1], b[m + 1];
    for(ll i = 1; i <= n; ++i)
      cin >> a[i];
    for(ll i = 1; i <= m; ++i)
      cin >> b[i];

    ll dp[n + 1][m + 1];
    for(ll j = 0; j <= m; ++j){
      dp[0][j] = j;
    }

    for(ll i = 1; i <= n; ++i){
      dp[i][0] = i;
      for(ll j = 1; j <= m; ++j){
        if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1];
        else dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1}); 
      }
    } 

    cout << dp[n][m];
  }
  return 0;
}
#include <iostream>
using namespace std;

/*
4 6
1 3 2 4
1 5 2 6 4 3
*/
int n,m;
int a[1005], b[1005];
int dp[1005][1005];

int main(){
	scanf("%d%d", &n,&m);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	for(int i=1; i<=m; i++) scanf("%d", b+i);

	for(int i=0; i<=m; i++) dp[0][i] = i;
	for(int i=0; i<=n; i++) dp[i][0] = i;

	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			dp[i][j] = min( dp[i-1][j]+1, min(dp[i][j-1]+1, dp[i-1][j-1] + (a[i]!=b[j])) );
		}
	}		
	
	cout<<dp[n][m];
	
	return 0;
}