#include <bits/stdc++.h>

using namespace std;

long lobg tab_1[2005], tab_2[2005], dp[2005][2005];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> tab_1[i];
    for (int i = 1; i <= m; i++)
        cin >> tab_2[i];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++){
            dp[i][j] = dp[i-1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
            if (tab_1[i] == tab_2[j])
                dp[i][j] += dp[i - 1][j - 1] + 1;
        }
    cout << dp[n][m] + 1 << "\n";
    return 0;
}