#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} else return false;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} else return false;}
struct Edge {int to; int w; Edge(int to, int w) : to(to), w(w) {}; };
using Graph = vector<vector<Edge>>;
const int INF = 1<<30;
const ll INFL = 1LL<<60;

int lcs (vector<int> x, vector<int> y) {
  int n = x.size();
  int m = y.size();
  vector<vector<int>> dp(n+1, vector<int>(m+1, INF));
  dp[0][0] = 0;
  rep(i, n+1) {
    rep(j, m+1) {
      if (j > 0) chmin(dp[i][j], dp[i][j-1] + 1);
      if (i > 0) chmin(dp[i][j], dp[i-1][j] + 1);
      if (i > 0 && j > 0) {
        int cost = 0;
        if (x[i-1] != y[j-1]) cost = 1;
        chmin(dp[i][j], dp[i-1][j-1]+cost);
      }
    }
  }
  return dp[n][m];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];
  int ans = lcs(a, b);
  cout << ans << endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define int long long int 
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) int a[n+f];for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define mii map<int,int>
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
#define N 1003

int dp[N][N],a[N],b[N];

int duck_duck_go(int idx1,int idx2,int n,int m)
{
	if(dp[idx1][idx2]!=-1) return dp[idx1][idx2];
	if(idx1==n || idx2==m) return (n-idx1)+(m-idx2);
	int res=inf;
	res=min(res,1+duck_duck_go(idx1+1,idx2,n,m));
	res=min(res,1+duck_duck_go(idx1,idx2+1,n,m));
	res=min(res,2+duck_duck_go(idx1+1,idx2+1,n,m));
	res=min(res,(a[idx1]!=b[idx2])+duck_duck_go(idx1+1,idx2+1,n,m));
	return dp[idx1][idx2]=res;
}


signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n,m;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>a[i];
	for(int i=0;i<m;i++)
		cin>>b[i];
	mem(dp,-1);
	cout<<duck_duck_go(0,0,n,m);	
	return 0;
}