#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, mod9 = 1000000009;
template <class T>
inline T lcm(T a, T b) {
  return (a * b / __gcd(a, b));
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  long long s[n], t[m];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }
  long long dp1[2100][2100], dp2[2100][2100];
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));
  dp1[0][0] = 1;
  for (int i = 0; i < 2100; i++) {
    dp2[0][i] = dp2[i][0] = 1;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i < n && j < m && s[i] == t[j]) {
        dp1[i + 1][j + 1] = dp2[i][j];
      }
      dp2[i + 1][j + 1] =
          dp2[i][j + 1] + dp2[i + 1][j] - dp2[i][j] + dp1[i + 1][j + 1];
    }
  }
  cout << dp2[n][m] << endl;
  return 0;
}
