#include <bits/stdc++.h>
int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a(n), b(m);
  for (int& x : a) std::cin >> x;
  for (int& x : b) std::cin >> x;
  constexpr int MOD = 1e9 + 7;
  std::vector<std::vector<int> > dp(n + 1, std::vector<int>(m + 1, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] + dp[i][j];
      if (a[i] == b[i]) dp[i + 1][j + 1]++;
    }
  }
  std::cout << dp[n][m] << '\n';
}
