#include <bits/stdc++.h>
using namespace std;
struct input {
  int num;
  int times;
  int pre;
};
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<input> s, t;
  int ts, tt;
  for (int i = 0; i < n; i++) {
    cin >> ts;
    s.push_back({ts, 0, -1});
  }
  for (int i = 0; i < m; i++) {
    cin >> tt;
    t.push_back({tt, 0, -1});
  }
  vector<int> tmp1(2 * 100000 + 1, 0);
  vector<int> tmp2(2 * 100000 + 1, -1);
  int maxk = 0;
  for (int i = 0; i < n; i++) {
    tmp1[s[i].num]++;
    s[i].pre = tmp2[s[i].num];
    tmp2[s[i].num] = i;
    s[i].times = tmp1[s[i].num];
    maxk = max(maxk, s[i].times);
  }
  for (int i = 0; i < 2 * 100000 + 1; i++) {
    tmp1[i] = 0;
    tmp2[i] = -1;
  }
  for (int i = 0; i < m; i++) {
    tmp1[t[i].num]++;
    t[i].pre = tmp2[t[i].num];
    tmp2[t[i].num] = i;
    t[i].times = tmp1[t[i].num];
    maxk = max(maxk, t[i].times);
  }
  vector<vector<long long>> dp(m + 1, vector<long long>(n + 1, 0));
  vector<vector<long long>> dp2(m + 1, vector<long long>(n + 1, 0));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (s[j].num == t[i].num) {
        dp[i + 1][j + 1] += dp[i][j] + 1;
      }
      dp[i + 1][j + 1] += dp[i][j + 1];
      dp[i + 1][j + 1] += dp[i + 1][j];
      dp[i + 1][j + 1] -= dp[i][j];
    }
  }
  cout << dp[m][n] + 1 << endl;
  return 0;
}
