#define pb push_back
#define mp make_pair
#define fi first
#define se second 
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 1699, INF = 2e9+6969;

int n,m,ans,dp[nax][nax],A[nax],B[nax];

int main()
{
	ans = INF;
	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=1;i<=n;++i) dp[i][0] = i;
	for(int i=1;i<=m;++i) dp[0][i] = i;
	
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<=m;++j)
		{
			dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + 1;
			dp[i][j] = min(dp[i][j], dp[i-1][j-1]+1);
			if(A[i] == B[j]) dp[i][j] = min(dp[i][j], dp[i-1][j-1]);
		}
	}
	
	//  for(int i=1;i<=n;++i)
	//  {
		//  for(int j=1;j<=m;j++) printf("%d ", dp[i][j]);
		//  puts("");
	//  }
	
	for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) ans = min(ans, dp[i][j] + n-i + m-j);
	printf("%d\n", ans);
	return 0;
}

#include <iostream>
#include <vector>

using namespace std;


class Solution{

private:
    int N, M;

public:
    int solve(int N, int M, vector<int>& A, vector<int>& B){

        this->N = N, this->M = M;
        vector<vector<int>> dp(N, vector<int>(M, -1));
        return dfs(A, B, 0, 0, dp);
    }

private:
    int dfs(const vector<int>& A, const vector<int>& B, int ai, int bi,
            vector<vector<int>>& dp){

        if(ai == A.size()) return B.size() - bi;
        if(bi == B.size()) return A.size() - ai;
        if(dp[ai][bi] != -1) return dp[ai][bi];

        int res = 1 + min(dfs(A, B, ai + 1, bi, dp), dfs(A, B, ai, bi + 1, dp));
        res = min(res, (A[ai] != B[bi]) + dfs(A, B, ai + 1, bi + 1, dp));
        return dp[ai][bi] = res;
    }
};


int main() {

    int N, M;
    cin >> N >> M;

    vector<int> A(N), B(M);
    for(int i = 0; i < N; i ++) cin >> A[i];
    for(int i = 0; i < M; i ++) cin >> B[i];
    cout << Solution().solve(N, M, A, B) << endl;

    return 0;
}
