#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int mod = 1e9 + 7;
int n, m, dp[2050][2050], ps[2050][2050], a[2050], b[2050];
int main() {
  memset(ps, 0, sizeof ps);
  ps[0][0] = ps[1][0] = ps[0][1] = 1;
  dp[0][0] = 1;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i and j) {
        if (a[i] == b[j])
          dp[i][j] = ps[i - 1][j - 1];
        else
          dp[i][j] = 0;
        ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + dp[i][j];
      } else {
        if (i == 0 and j != 0)
          ps[i][j] = ps[i][j - 1];
        else if (j == 0 and i != 0)
          ps[i][j] = ps[i - 1][j];
      }
    }
  }
  cout << ps[n][m] << endl;
  return 0;
}
