#include <bits/stdc++.h>
using namespace std;
int n, m, a[210000], b[210000], c[210000];
int f[210000], big[210000], sma[210000], nex[210000], las[210000];
int main() {
  cin >> n >> m;
  for (int i = (1); i <= (n); i++) {
    int x;
    scanf("%d", &x);
    a[x] = i;
    c[i] = x;
    nex[i] = i + 1;
    las[i] = i - 1;
  }
  for (int i = (n); i >= (1); i--) {
    int x = a[i];
    int y = las[x], z = nex[x];
    big[i] = c[z];
    sma[i] = c[y];
    nex[y] = z;
    las[z] = y;
  }
  f[1] = 0;
  int k = 0;
  for (int i = (2); i <= (n); i++) {
    while (k) {
      if (!big[k + 1] || big[k + 1] && a[i] < a[i - (k + 1 - big[k + 1])])
        if (!sma[k + 1] || sma[k + 1] && a[i] > a[i - (k + 1 - sma[k + 1])])
          break;
      k = f[k];
    }
    if (!big[k + 1] || big[k + 1] && a[i] < a[i - (k + 1 - big[k + 1])])
      if (!sma[k + 1] || sma[k + 1] && a[i] > a[i - (k + 1 - sma[k + 1])]) k++;
    f[i] = k;
  }
  for (int i = (1); i <= (m); i++) {
    int x;
    scanf("%d", &x);
    b[x] = i;
  }
  int ans = 0;
  k = 0;
  for (int i = (1); i <= (m); i++) {
    while (k) {
      if (!big[k + 1] || big[k + 1] && b[i] < b[i - (k + 1 - big[k + 1])])
        if (!sma[k + 1] || sma[k + 1] && b[i] > b[i - (k + 1 - sma[k + 1])])
          break;
      k = f[k];
    }
    if (!big[k + 1] || big[k + 1] && b[i] < b[i - (k + 1 - big[k + 1])])
      if (!sma[k + 1] || sma[k + 1] && b[i] > b[i - (k + 1 - sma[k + 1])]) k++;
    if (k == n) ans++, k = f[k];
  }
  cout << ans << endl;
  return 0;
}
