#include <bits/stdc++.h>
void Get(int &T) {
  char C;
  bool F = 0;
  for (; C = getchar(), C < '0' || C > '9';)
    if (C == '-') F = 1;
  for (T = C - '0'; C = getchar(), C >= '0' && C <= '9'; T = T * 10 + C - '0')
    ;
  F && (T = -T);
}
int N, M;
int A[200005];
int B[200005];
int InvA[200005];
int InvB[200005];
void Init() {
  Get(M);
  Get(N);
  for (int i = 1; i <= M; i++) {
    Get(B[i]);
  }
  for (int i = 1; i <= N; i++) {
    Get(A[i]);
  }
  for (int i = 1; i <= N; i++) InvA[A[i]] = i;
  for (int i = 1; i <= M; i++) InvB[B[i]] = i;
}
int Min[200005];
int Max[200005];
int Q[200005];
int L, R;
int Next[200005];
void Init_KMP() {
  Next[1] = 0;
  for (int i = 2; i <= M; i++) {
    for (int j = Next[i - 1];; j = Next[j]) {
      bool OK = 1;
      if (Max[j + 1] != 0 && InvB[Max[j + 1] - j - 1 + i] < InvB[i]) OK = 0;
      if (Min[j + 1] != 0 && InvB[Min[j + 1] - j - 1 + i] > InvB[i]) OK = 0;
      if (OK == 1) {
        Next[i] = j + 1;
        break;
      }
      if (j == 0) {
        Next[i] = j;
        break;
      }
    }
  }
}
int Ans;
void KMP() {
  for (int i = 1, j = 0; i <= N; i++) {
    for (;; j = Next[j]) {
      bool OK = 1;
      if (Max[j + 1] != 0 && InvA[Max[j + 1] - j - 1 + i] < InvA[i]) OK = 0;
      if (Min[j + 1] != 0 && InvA[Min[j + 1] - j - 1 + i] > InvA[i]) OK = 0;
      if (OK == 1) {
        j++;
        if (j == M) {
          Ans++;
          j = Next[j];
        }
        break;
      }
      if (j == 0) {
        break;
      }
    }
  }
}
void Work() {
  Q[L = R = 1] = 0;
  for (int i = 1; i <= M; i++) {
    for (; L <= R && B[Q[R]] > B[i];) R--;
    Min[B[i]] = B[Q[R]];
    Q[++R] = i;
  }
  Q[L = R = 1] = 0;
  for (int i = M; i >= 1; i--) {
    for (; L <= R && B[Q[R]] > B[i];) R--;
    Max[B[i]] = B[Q[R]];
    Q[++R] = i;
  }
  Init_KMP();
  KMP();
}
void Output() { printf("%d\n", Ans); }
int main() {
  Init();
  Work();
  Output();
  return 0;
}
