#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int dp[2004][2004];
int acc[2004][2004];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), t(m);
  for (int i = 0; i < (n); ++i) cin >> s[i];
  for (int i = 0; i < (m); ++i) cin >> t[i];
  dp[0][0] = 1;
  acc[1][1] = 1;
  for (int i = 0; i < (n + 1); ++i)
    for (int j = 0; j < (m + 1); ++j) {
      if (i + j == 0) continue;
      if (i && j && s[i - 1] == t[j - 1]) dp[i][j] = acc[i][j];
      acc[i + 1][j + 1] = acc[i + 1][j] + acc[i][j + 1] - acc[i][j] + dp[i][j];
    }
  cout << acc[n + 1][m + 1] << endl;
}
