#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

ll dp[1111][1111],a[1111],b[1111];

int main(void){
	int i,j,n,m;
	std::fill(dp[0],dp[1111],INFLL);
	dp[0][0] = 0ll;
	std::cin >> n >> m;
	for(i=0; i<n; ++i){
		std::cin >> a[i];
	}
	for(i=0; i<m; ++i){
		std::cin >> b[i];
	}
	for(i=0; i<=n; ++i){
		for(j=0; j<=m; ++j){
			if(i<n){
				dp[i+1][j] = std::min(dp[i+1][j],dp[i][j]+1);
			}
			if(j<m){
				dp[i][j+1] = std::min(dp[i][j+1],dp[i][j]+1);
			}
			if(i<n && j<m){
				dp[i+1][j+1] = std::min(dp[i+1][j+1],dp[i][j]+((a[i]==b[j])?0:1));
			}
			// std::cout << i << " " << j << " " << dp[i][j] << std::endl;
		}
	}
	std::cout << dp[n][m] << std::endl;
	return 0;
}

// Problem: E - Sequence Matching
// Contest: AtCoder - AtCoder Beginner Contest 185
// URL: https://atcoder.jp/contests/abc185/tasks/abc185_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define du double
#define jmp "\n"
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rp(i, s, e) for (ll i = s; i <= e; i++)
#define vl vector<ll>
#define vvl vector<vl>
#define pb push_back
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll, vi, greater<ll>>
#define printv(v)         \
    for (auto x : v)      \
        cout << x << " "; \
    cout << jmp;
#define vi vector<int>
#define vb vector<bool>
#define ump unordered_map
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define all(x) x.begin(), x.end()
#define SET(x, a) memset(x, a, sizeof(x))
#define si(x) (ll) x.size()
#define ff first
#define ss second
#define I(a)          \
    for (auto &x : a) \
        cin >> x;
#define iin insert
#define log(args...)                             \
    {                                            \
        string _s = #args;                       \
        replace(_s.begin(), _s.end(), ',', ' '); \
        stringstream _ss(_s);                    \
        istream_iterator<string> _it(_ss);       \
        err(_it, args);                          \
    }
#define logarr(arr, a, b)            \
    for (int z = (a); z <= (b); z++) \
        cout << (arr[z]) << " ";     \
    cout << endl;                    \
    using namespace std;
using namespace std;

// vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
    cout << *it << " = " << a << endl;
    err(++it, args...);
}
const ll nax = 1e3 + 5;
ll dp[nax][nax], a[nax], b[nax];
ll n, m;
ll r(ll i, ll j)
{

    if (i == -1 && j == -1)
        return 0;
    if (i == -1)
        return j + 1;
    if (j == -1)
        return i + 1;

    ll &mem = dp[i][j];
    if (mem != -1)
        return mem;
    ll aa, bb, c;
    aa = (a[i] != b[j]) + r(i - 1, j - 1);
    bb = 1 + r(i, j - 1);
    c = 1 + r(i - 1, j);
    return mem = min({aa, bb, c});
}
void solve()
{
    cin >> n >> m;
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> b[i];
    SET(dp, -1);
    cout << r(n - 1, m - 1);
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}