#include <bits/stdc++.h>

using namespace std;

int f[1010][1010], a[1010], b[1010], n, m;

int main() {
	cin >> n >> m;
	for(register int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for(register int j = 1; j <= m; j++) {
		cin >> b[j];
	}
	for(register int i = 1; i <= m; i++) f[0][i] = i;
	for(register int i = 1; i <= n; i++) f[i][0] = i;
	for(register int i = 1; i <= n; i++) {
		for(register int j = 1; j <= m; j++) {
			f[i][j] = min(f[i - 1][j], f[i][j - 1]) + 1;
			if(a[i] == b[j]) f[i][j] = min(f[i][j], f[i - 1][j - 1]);
			else f[i][j] = min(f[i][j], f[i - 1][j - 1] + 1);
		}
	}
	cout << f[n][m] << endl;
	return 0;
}
/*   AUTHOR: julianferres   */
#include <bits/stdc++.h>
using namespace std;

// neal Debugger
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
 
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

typedef long long ll;
typedef vector<ll> vi; typedef pair<ll,ll> ii;
typedef vector<ii> vii; typedef vector<bool> vb;
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define DBG(x) cerr << #x << " = " << (x) << endl
#define RAYA cerr << "===============================" << endl
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define esta(x,c) ((c).find(x) != (c).end())
const int INF = 1e9+15; // const ll INF = 2e18;
const int MOD = 1e9+7; // 998244353
const int MAXN  = 1005;

vi a(MAXN), b(MAXN);
vector<vi> dp(MAXN, vi(MAXN, -1));


ll solve(ll n,ll m){
    if(dp[n][m]!=-1)
        return dp[n][m];
    if(n==0 || m==0)
        return max(n,m);
    dp[n][m] = min({solve(n-1,m-1)+((ll)(a[n]!=b[m])),solve(n,m-1)+1,solve(n-1,m)+1});

    return dp[n][m];
}

int main(){
	FIN;

    int n, m; cin >> n >> m;

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

    cout << solve(n,m) << endl;

    return 0;
}