#include <bits/stdc++.h>
using namespace std;
const int maxn = 400010;
int n, m;
int ans;
int A[maxn], B[maxn], Ia[maxn], Ib[maxn];
int pre[maxn], suc[maxn], las[maxn], the[maxn];
int jump[maxn];
int cmp(int *A, int a, int b) {
  return (las[a] == 0 || A[b - a + las[a]] < A[b]) &&
         (the[a] == 0 || A[b - a + the[a]] > A[b]);
}
int main() {
  int i, j;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= n; ++i) scanf("%d", &Ia[i]), A[Ia[i]] = i;
  for (i = 1; i <= m; ++i) scanf("%d", &Ib[i]), B[Ib[i]] = i;
  for (i = 0; i <= n; ++i) pre[suc[i] = i + 1] = i;
  for (i = n; i >= 1; --i) {
    las[i] = Ia[pre[A[i]]], the[i] = Ia[suc[A[i]]];
    pre[suc[A[i]]] = pre[A[i]], suc[pre[A[i]]] = suc[A[i]];
  }
  for (i = 2, j = 0; i <= n; ++i) {
    for (; j && !cmp(A, j + 1, i); j = jump[j])
      ;
    j += cmp(A, j + 1, i);
    jump[i] = j;
  }
  for (i = 1, j = 0; i <= m; ++i) {
    for (; j && !cmp(B, j + 1, i); j = jump[j])
      ;
    j += cmp(B, j + 1, i);
    if (j == n) ++ans, j = jump[j];
  }
  printf("%d", ans);
  return 0;
}
