#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  int ar[n + 2];
  for (int i = 1; i <= n; i++) cin >> ar[i];
  int br[m + 2];
  for (int j = 1; j <= m; j++) cin >> br[j];
  int dp[n + 2][m + 2];
  memset(dp, 0, sizeof dp);
  dp[0][0] = 1;
  dp[0][1] = 1;
  dp[1][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
      if (ar[i] == br[j]) {
        dp[i][j] += (dp[i - 1][j - 1] + 1);
      }
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}
