#include <bits/stdc++.h>
int a[200005], b[200005], p[200005], L[200005], R[200005], Sm[200005],
    Bg[200005], next[200005], n, m, x, ans;
bool kmp(int *a, int i, int j) {
  if (Sm[j] && a[i - j + Sm[j]] > a[i]) return 0;
  if (Bg[j] && a[i - j + Bg[j]] < a[i]) return 0;
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]), a[p[i]] = i, L[i] = i - 1, R[i] = i + 1;
  for (int i = 1; i <= m; i++) scanf("%d", &x), b[x] = i;
  for (int i = n; i; i--) {
    x = a[i], Sm[i] = p[L[x]], Bg[i] = p[R[x]], L[R[x]] = L[x], R[L[x]] = R[x];
  }
  for (int i = 2, j = 0; i <= n; i++) {
    while (j && !kmp(a, i, j + 1)) j = next[j];
    if (kmp(a, i, j + 1)) j++;
    next[i] = j;
  }
  for (int i = 1, j = 0; i <= m; i++) {
    while (j && !kmp(b, i, j + 1)) j = next[j];
    if (kmp(b, i, j + 1)) j++;
    if (j == n) ans++, j = next[j];
  }
  printf("%d\n", ans);
  return 0;
}
