#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  long long mod = 1e9 + 7;
  vector<long long> S(N + 1, 0), T(M + 1, 0);
  for (int i = 1; i <= N; i++) cin >> S[i];
  for (int i = 1; i <= M; i++) cin >> T[i];
  vector<vector<long long>> dp(N + 1, vector<long long>(M + 1, 0));
  dp[0][0] = 1;
  for (int n = 1; n <= N; n++)
    for (int m = 1; m <= M; m++) {
      if (S[n] != T[m]) continue;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
          dp[n][m] += dp[i][j];
        }
    }
  long long ans = 0;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= M; j++) {
      ans += dp[i][j];
    }
  cout << ans << endl;
}
