#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
 
#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(int i=cc;i>=n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;
 
const vector<int> dx = {1, 0, -1,  0};
const vector<int> dy = {0, 1,  0, -1};
 
const vector<int> dx2 = {1, 1,  1, 0,  0, 0, -1, -1, -1};
const vector<int> dy2 = {1, 0, -1, 1, -1, 0,  1,  0, -1};
 
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;
 
const ll MAX = 1000000;
const int MOD = 1000000007;
const ll big = (1ll<<60);
 
ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }
ll modPow(ll a, ll n, ll p) {
  if (n == 0) return 1;
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}

int main(){
  int n, m;
  cin >> n >> m;
  vector<ll>a(n),b(m);
  vector<vector<ll>>dp(n+1,vector<ll>(m+1,0));
  rep(i, 0, n)cin >> a[i];
  rep(i, 0, m)cin >> b[i];
  rep(i, 0, n+1)dp[i][0] = i;
  rep(i, 0, m+1)dp[0][i] = i;
  rep(i, 1, n+1){
    rep(j, 1, m+1){
      if(a[i-1]==b[j-1])dp[i][j] = min({dp[i-1][j]+1,dp[i][j-1]+1,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});
    }
  }
  /*
  rep(i, 0, n+1){
    rep(j, 0, m+1){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  cout << dp[n][m] << endl;
}
/* � Ashiq Uddin Pranto
   Department of Computer Science and Engineering
   BATCH 27
   University Of Rajshahi,Bangladesh
*/
#include<bits/stdc++.h>
 
#define ll long long
#define ld long double
#define FOR(x,y) for(ll i=x;i<=y;i++)
#define pb(x) push_back(x)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define matrix(x) vector<vector<x>>
#define vss vector<string>
#define PI (2*acos(0.0))
#define sqr(x) ((x)*(x))
#define sf(a)   scanf("%d",&a)
#define sfl(a)  scanf("%lld",&a)
#define sff(a,b)    scanf("%d %d",&a,&b)
#define sffl(a,b)   scanf("%lld %lld",&a,&b)
#define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)    scanf("%lld %lld %lld",&a,&b,&c)
#define ms(a,b) memset(a, b, sizeof(a))
#define ff  first
#define ss  second
#define lcm(a, b)   ((a)*((b)/__gcd(a,b)))
#define all(a)  a.begin(),a.end()
#define readfile freopen("input.txt","r",stdin);
#define writefile freopen("output.txt","w",stdout);
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define gap " "
#define mx 104
#define inf (ll)1e9
#define WHITE 1
#define GRAY 2
#define BLACK 3
#define EMPTY_VALUE -1
#define mod 1000000007
#define MOD(a,b) (a%b + b)%b
 
using namespace std;
 
void __print(int 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

ll n,m;
ll dp[1005][1005];
ll a[1005],b[1005];

ll solve(ll i,ll j)
{
    if(dp[i][j]!=-1) return dp[i][j];
    if(i>=n && j>=m) return 0;
    if(i>=n) return (m-j);
    if(j>=m) return (n-i);
    ll ans = inf;
    if(a[i]==b[j])
    {
        ans = min(ans,solve(i+1,j+1));
    }
    ans=min(ans,1+solve(i+1,j));
    ans=min(ans,1+solve(i,j+1));
    ans=min(ans,1+solve(i+1,j+1));
    return dp[i][j] = ans;

}

void eff()
{
    memset(dp,-1,sizeof(dp));
    sffl(n,m);
    for(ll i=0;i<n;i++) sfl(a[i]);
    for(ll i=0;i<m;i++) sfl(b[i]);
    printf("%lld\n",solve(0,0));

}
 
int main()
{
    // fastio
    // ll test ;
    // sfl(test);
    // for(ll i=0;i<test;i++)
    // {
        // printf("Case %lld: ",i+1);
        eff();
    // }
//    cout<<check(81);
    return 0;
}
