#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
long long s[2005], t[2005];
long long dp[2005][2005];
signed main() {
  long long n, m;
  cin >> n >> m;
  dp[0][0] = 1;
  dp[0][1] = 1;
  dp[1][0] = 1;
  for (long long i = 1; i <= n; i++) {
    cin >> s[i];
    dp[i][0] = 1;
  }
  for (long long j = 1; j <= m; j++) {
    cin >> t[j];
    dp[0][j] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (s[i] == t[j])
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]);
      else
        dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]);
    }
  }
  cout << dp[n][m] << endl;
}
