#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int a[MAXN], b[MAXN], ra[MAXN], rb[MAXN], pre[MAXN], suc[MAXN];
int pv[MAXN], sv[MAXN], nxt[MAXN];
int i, j, k, N, M, x, y, ans;
void init() {
  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++) pv[sv[i] = i + 1] = i;
}
inline bool cmpA(int x, int y) {
  return ((!pre[x]) || (a[y - x + pre[x]] < a[y])) &&
         ((!suc[x]) || (a[y - x + suc[x]] > a[y]));
}
inline bool cmpB(int x, int y) {
  return ((!pre[x]) || (b[y - x + pre[x]] < b[y])) &&
         ((!suc[x]) || (b[y - x + suc[x]] > b[y]));
}
void work() {
  for (i = N; i; i--) {
    pre[i] = ra[pv[a[i]]], pv[sv[a[i]]] = pv[a[i]];
    suc[i] = ra[sv[a[i]]], sv[pv[a[i]]] = sv[a[i]];
  }
  for (i = 2; i <= N; i++) {
    for (; k && !cmpA(k + 1, i); k = nxt[k])
      ;
    k += cmpA(k + 1, i);
    nxt[i] = k;
  }
  for (i = 1, k = 0; i <= M; i++) {
    for (; k && !cmpB(k + 1, i); k = nxt[k])
      ;
    if ((k += cmpB(k + 1, i)) >= N) ans++, k = nxt[k];
  }
  printf("%d\n", ans);
}
int main() {
  init();
  work();
  return 0;
}
