#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 998244353;
int n, m;
int f[3005][3005];
int main() {
	cin >> n >> m;
	f[1][1] = 1;
	for (int i = 2; i <= n; ++i) {
		for (int j = i; j; --j) {
			f[i][j] = f[i - 1][j - 1];
			if (j + j <= i) {
				f[i][j] += f[i][j + j];
				if (f[i][j] >= mod) f[i][j] -= mod;
			}
		}
	}
	cout << f[n][m];
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define itn int
#define rep2(i,a,n) for(int i = (a);i < (n);i++)
#define rep(i,n) rep2(i,0,n)
#define P pair<int,int>
#define endl '\n'

int dp[1001][1001];

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n,m;
    cin >> n >> m;
    int a[n],b[m];
    rep(i,n)cin >> a[i];
    rep(i,m)cin >> b[i];
    
    rep(i,n+1){
        rep(j,m+1){
            dp[i][j] = 1e9;
        }
    }
    
    dp[0][0] = 0;
    
    rep(i,n+1){
        rep(j,m+1){
            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){
                if(a[i-1] == b[j-1])dp[i][j] = min(dp[i][j],dp[i-1][j-1]);
                else dp[i][j] = min(dp[i][j],dp[i-1][j-1] + 1);
            }
        }
    }
    
    cout << dp[n][m] << endl;
}