#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long INF = 1000000000;
long double EPS = 1e-11;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T>& vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}
using Graph = vector<vector<long long>>;
long long dp1[2010][2010];
long long dp2[2010][2010];
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> S(N + 1), T(M + 1);
  for (long long i = (long long)0; i < (long long)N; i++) cin >> S[i];
  for (long long i = (long long)0; i < (long long)M; i++) cin >> T[i];
  dp1[0][0] = 1;
  for (long long i = (long long)0; i < (long long)N + 1; i++)
    for (long long j = (long long)0; j < (long long)M + 1; j++) {
      dp1[i + 1][j] += dp1[i][j];
      dp2[i][j] += dp1[i][j];
      dp2[i][j + 1] += dp2[i][j];
      if (S[i] == T[j]) {
        dp1[i + 1][j + 1] += dp2[i][j];
      }
    }
  cout << dp2[N][M] << endl;
}
