#include <bits/stdc++.h>
int N, M, i, p, ans, ra[200010], rb[200010], a[200010], b[200010], prev[200010],
    next[200010], pre[200010], suc[200010], shift[200010];
bool cmp(int *b, int p, int i) {
  return (pre[p] == 0 || b[i - p + pre[p]] < b[i]) &&
         (suc[p] == 0 || b[i - p + suc[p]] > b[i]);
}
int main() {
  scanf("%d%d", &N, &M);
  for (i = 1; i <= N; ++i) scanf("%d", ra + i), a[ra[i]] = i;
  for (i = 1; i <= M; ++i) scanf("%d", rb + i), b[rb[i]] = i;
  for (i = 0; i <= N; ++i) prev[next[i] = i + 1] = i;
  for (i = N; i; --i)
    pre[i] = ra[prev[a[i]]], suc[i] = ra[next[a[i]]],
    prev[next[a[i]]] = prev[a[i]], next[prev[a[i]]] = next[a[i]];
  for (i = 2; i <= N; ++i) {
    while (p && !cmp(a, p + 1, i)) p = shift[p];
    shift[i] = p += cmp(a, p + 1, i);
  }
  for (p = 0, i = 1; i <= M; ++i) {
    while (p && !cmp(b, p + 1, i)) p = shift[p];
    if ((p += cmp(b, p + 1, i)) == N) ++ans, p = shift[p];
  }
  printf("%d", ans);
  return 0;
}
