#include <bits/stdc++.h>

#define INF 1e9
#define LINF (1LL << 63 - 1)
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a)  (a).begin(),(a).end()
// #define chmax(a, b) a = max(a, b)
// #define chmin(a, b) a = min(a, b)
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())

using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;

template<typename T> bool chmin(T& a, const T& b) { if(a > b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a < b){ a = b; return 1;} return 0; }

const ll ZER = 0;
const ll MOD = 1e9 + 7;


ll dp[2020][2020];
int main(){
    // std::cin.tie(0);
    // std::ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    vector<ll> a(n), b(m);
    rep(i, n)cin >> a[i];
    rep(i, m)cin >> b[i];
    rep(i, n)dp[i + 1][0] = i + 1;
    rep(j, m)dp[0][j + 1] = j + 1;
    rep(i, n){
        rep(j, m){
            if(a[i] == b[j]){
                dp[i + 1][j + 1] = dp[i][j];
            }
            else {
                dp[i + 1][j + 1] = min({dp[i][j], dp[i + 1][j], dp[i][j + 1]}) + 1;
            }
        }
    }
    cout << dp[n][m] << endl;
}
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <list>
#include <unordered_set>

#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
void ex_gcd(ll a,ll b, ll& d,ll& x,ll& y){
  if(!b) {d=a;x=1;y=0;}
  else   {	ex_gcd(b,a%b,d,y,x);	y-=a/b*x;}
}
ll inv(ll a,ll n)  {
  ll d,x,y;	ex_gcd(a,n,d,x,y);
  return d==1?(x%n+n)%(n/d):-1;
}
const int maxn = 1005;
const int mod = 1000000007;

int a[maxn], b[maxn];
int f[maxn][maxn];

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  //freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n,m;
  cin>>n>>m;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=m;i++)cin>>b[i];
  int ret = 0;
  for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++){
      f[i][j] = max(f[i-1][j],f[i][j-1]);
      f[i][j] = max(f[i][j],f[i-1][j-1]+2-(a[i]!=b[j]));
      ret = max(ret, f[i][j]);
    }
  cout<<n+m-ret<<endl;
  return 0;
}