#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int a[1001], b[1001];
int n, m;

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

int main() {
    memset(dp, -1, sizeof dp);
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
    }
    cout << solve(0, 0) << endl;
    return 0;
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>  // Common file
#include <ext/pb_ds/tree_policy.hpp>  // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define nrep(i, a, b) for (__typeof((b)) i = (a); i > (b); i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define ppi pair<int, int>
#define pppi pair<ppi, int>
#define vi vector<int>
#define vii vector<ppi>
#define viii vector<pppi>
#define vs vector<string>
#define pb push_back
#define bitcount __builtin_popcountll  // count no's of ones in a set bit
#define prq priority_queue
#define mp make_pair
#define mem(x, val) memset((x), (val), sizeof(x));
#define sz(x) (int)x.size()
#define M_PI 3.14159265358979323846
#define rootdada                    \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0);
#define test \
  int t;     \
  cin >> t;  \
  while (t--)
#define debug(a)                     \
  cerr << #a << ": ";                \
  for (auto i : a) cerr << i << " "; \
  cerr << '\n';
#define trace(a) cerr << #a << ": " << a << "\n"
#define print(ar, n)            \
  for (int u = 0; u < n; u++) { \
    cout << ar[u] << " ";       \
  }                             \
  cout << endl;
#define int long long
#define M 1000000007
#define ordered_set                            \
  tree<int, null_type, less<int>, rb_tree_tag, \
       tree_order_statistics_node_update>
int lcs( vector<int> X, vector<int> Y, int n, int m )
{
	int L[n + 1][m + 1];
	int i, j;

	/* Following steps build L[m+1][n+1] in
	   bottom up fashion. Note that L[i][j]
	   contains length of LCS of X[0..i-1]
	   and Y[0..j-1] */
	for (i = 0; i <= n; i++)
		L[i][0] = i;
	for (j = 0; j <= m; j++)
		L[0][j] = j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < m; j++)
		{
			if (X[i] == Y[j])
				L[i + 1][j + 1] = L[i][j];

			else
				L[i + 1][j + 1] = min(L[i][j], min(L[i + 1][j], L[i][j + 1])) + 1;
		}
	}

	/* L[m][n] contains length of LCS
	for X[0..n-1] and Y[0..m-1] */
	return L[n][m];
}
signed main() {
	rootdada;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("error.txt", "w", stderr);
	freopen("output.txt", "w", stdout);
#endif
	int n, m;
	cin >> n >> m;
	vi a(n), b(m);
	rep(i, 0, n)
	cin >> a[i];
	rep(i, 0, m)
	cin >> b[i];
	int ans = lcs(a, b, n, m);
	cout << ans << endl;

}