#include <bits/stdc++.h>
constexpr int kMod = 1000000007;
constexpr int kMax = 2001;
long long dp[kMax][kMax];
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> S(N), T(M);
  for (int i = 0; i < N; ++i) std::cin >> S[i];
  for (int j = 0; j < M; ++j) std::cin >> T[j];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (S[i] != T[j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = 1;
        for (int a = 0; a < i; ++a) {
          for (int b = 0; b < j; ++b) {
            dp[i][j] += dp[a][b];
          }
        }
      }
    }
  }
  long long total = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      total += dp[i][j];
    }
  }
  std::cout << total + 1 << std::endl;
}
