#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

const int N = 1000 + 5, mod = 1e9 + 7;


int n, m, a[N], b[N], dp[N][N];

int solve(int idx_a, int idx_b){
    if(idx_a == n) return m - idx_b;
    if(idx_b == m) return n - idx_a;
    if(dp[idx_a][idx_b] != -1) return dp[idx_a][idx_b];
    int op1 = 1 + solve(idx_a + 1, idx_b);
    int op2 = 1 + solve(idx_a, idx_b + 1);
    int op3 = solve(idx_a + 1, idx_b + 1);
    if(a[idx_a] != b[idx_b]) op3++;
    return dp[idx_a][idx_b] = min({op1, op2, op3});
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < m; i++) cin >> b[i];
    memset(dp, -1, sizeof dp);
    cout << solve(0, 0);

    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fon(i,n) for(int i=0;i<n;i++)
#define fo(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll ,ll>
#define vii vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define vpll vector<pll>
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define M (1000*1000*1000+7)
#define test ll T; cin>>T; while(T--)
#define all(z) z.begin() , z.end()
#define allr(z) z.rbegin() , z.rend()
// =============================================
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9) // 2.runtime error //3.boundary condition

int a[1002] , b[1002];
int dp[1002][1002];
int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   
   memset(dp , 0 , sizeof(dp));
   int n , m;
   cin >> n >> m;
//    int a[n] , b[n];

   fon(i ,n) cin >> a[i];
   fon(i , m)cin >> b[i];

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

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

   cout << dp[n][m] << endl;


}