/*
 * @Author: AsilenceBTF
 * @LastEditTime: 2020-12-15 13:39:37
 */
#include<bits/stdc++.h>
using namespace std;
 
#define ALL(x) x.begin(), x.end()
typedef long long ll;
const int INF = 0x3f3f3f3f;

const int N = 1e3 + 105;
int a[N], b[N], dp[N][N];
int main(){
    int n, m; 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 = 0; i <= n; ++i){
        for(int j = 0; j <= m; ++j){
            if(i == 0 && j == 0) continue;
            dp[i][j] = INF;
            if(i > 0){
                dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
            }
            if(j > 0){
                dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
            }
            if(i > 0 && j > 0){
                dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (a[i] != b[j]));
            }
        }
    }
    printf("%d\n", dp[n][m]);
    // system("pause");
}
#include<bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef std::vector<vector<int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<bool> vb;
typedef vector<vb> vvb;


#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repi(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a.size())
#define umin(a, x) a = min(a, x)
#define umax(a, x) a = max(a, x)

ll nxt() {
	ll x; cin >> x; return x;
}

void setIO(string s) {
	#ifndef LOCAL
	ios_base::sync_with_stdio(0); cin.tie(0); 
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
	#endif
}

bool done[1005][1005];

int dp[1005][1005];

int a[1005], b[1005];

int n, m;

const int inf = 1e9;

int go(int x, int y) {
	if (x >= 0 && y >= 0 && x <= n && y <= m) {
		if (!done[x][y]) {
			if (x == 0) {
				dp[x][y] = y;
			} else if (y == 0) {
				dp[x][y] = x;
			} else {
				int one = (int)(a[x] != b[y]) + go(x - 1, y - 1);
				int two = 1 + go(x - 1, y);
				int three = 1 + go(x, y - 1);
				int four = 2 + go(x - 1, y - 1);
				dp[x][y] = min({one, two, three, four});
			}

			done[x][y] = 1;
			return dp[x][y];
		} else {
			return dp[x][y];
		}
	} else {
		return inf;
	}
}

void solve() {
	cin >> n >> m;

	rep(i, 1, n + 1) cin >> a[i];
	rep(i, 1, m + 1) cin >> b[i];

	go(n, m);
	cout << dp[n][m] << "\n";
}	
	
int32_t main(){
	// setIO("trapped");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cout << fixed << setprecision(10);

	int T = 1;
	// cin >> T;

	for (int tc = 1; tc <= T; tc++) {
		// cout << "Case #" << tc << ": ";
		solve();
	}
}
