#include <bits/stdc++.h>
using namespace std;
long long dp[2010][2010];
long long sum[2010][2010];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long s[n + 1];
  long long t[m + 1];
  for (long long i = 1; i <= n; i++) cin >> s[i];
  for (long long i = 1; i <= m; i++) cin >> t[i];
  dp[0][0] = 1;
  for (long long i = 1; i <= n; i++) {
    dp[i][0] = 0;
    sum[i][0] = 1;
  }
  for (long long i = 1; i <= m; i++) {
    dp[0][i] = 0;
    sum[0][i] = 1;
  }
  sum[0][0] = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      if (s[i] != t[j]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = sum[i - 1][j - 1] + 1;
      }
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + dp[i][j];
    }
  }
  cout << sum[n][m];
}
