#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_E"


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

//BEGIN CUT HERE
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//END CUT HERE


using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pii = pair<int, int>;
using pld = pair<ll, ld>;
using ppiii =  pair<pii, int>;
using ppiill = pair<pii, ll>;
using ppllll = pair<pll, ll>;
using pplii = pair<pli, int>;
using mii = map<int, int>;
using dll = deque<ll>;
using qll = queue<ll>;
using pqll = priority_queue<ll>;
using pqrll = priority_queue<ll, vector<ll>, greater<ll>>;
using vint = vector<int>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vvll = vector<vector<ll>>;
using vvint = vector<vector<int>>;
using vvpll = vector<vector<pll>>;

//マクロ
//forループ
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define ALL(x) x.begin(),x.end() 
#define rALL(x) x.rbegin(),x.rend() 
#define SIZE(x) ll(x.size()) 
#define fs first
#define sc second
#define INF32 2147483647 //2.147483647x10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807x10^{18}:64bit整数のinf
//定数



signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  ll N,M;
  cin>>N>>M;

  vll A(N),B(M);
  REP(i,N) cin >> A[i];
  REP(i,M) cin >> B[i];

    vvll dp(N+1,vll(M+1,INF32));
    dp[0][0] = 0;

    REP(i,N+1){
        REP(j,M+1){
            if(i>0&&j>0){
                if(A[i-1]==B[j-1])chmin(dp[i][j],dp[i-1][j-1]);
                else chmin(dp[i][j],dp[i-1][j-1]+1);
            }
            if(i>0)chmin(dp[i][j],dp[i-1][j]+1);
            if(j>0)chmin(dp[i][j],dp[i][j-1]+1);
        }
    }



    cout << dp[N][M] << endl;


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

#define endl '\n'

const int N = 3e5 + 5;

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

int memo(int i, int j) {
	if(i == -1 && j == -1) return 0;
	if(i == -1) return j+1;
	if(j == -1) return i+1;
	if(dp[i][j] != -1) return dp[i][j];
	int res = min(memo(i-1, j), memo(i, j-1)) + 1;
	if(a[i] == b[j]) res = min(res, memo(i-1, j-1));
	else res = min(res, memo(i-1, j-1) + 1);
	return dp[i][j] = res;
}

int main() {
	ios::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 << memo(n-1, m-1) << endl;

	return 0;
}