#include <bits/stdc++.h>
using namespace std;
int m, n, j, k, l, i, o, p, __t, Fail[230000];
pair<int, int> Pattern[230000], Match[230000], Lef[230000], Rig[230000];
char ch;
void read(int &a) {
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-')
    __t = -1, a = 0;
  else
    a = ch - '0', __t = 1;
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    a = a * 10 + ch - '0';
  a = a * __t;
}
void Read(int m, pair<int, int> *Base) {
  for (int i = (1); i <= (m); i++) {
    read(Base[i].first);
    Base[Base[i].first].second = i;
  }
}
bool Check(int i, int j, pair<int, int> *A) {
  return ((Lef[j + 1].second != 0 &&
           A[i].second < A[i - Lef[j + 1].second].second) ||
          (Rig[j + 1].second != 0 &&
           A[i].second > A[i - Rig[j + 1].second].second));
}
int main() {
  read(m), read(n);
  Read(m, Match);
  Read(n, Pattern);
  for (int i = (1); i <= (m); i++) Lef[i].first = i - 1, Rig[i].first = i + 1;
  for (int i = (m); i >= (1); i--) {
    int P = Match[i].second;
    if (Lef[P].first > 0)
      Lef[i].second = i - Match[Lef[P].first].first,
      Rig[Lef[P].first].first = Rig[P].first;
    if (Rig[P].first <= m)
      Rig[i].second = i - Match[Rig[P].first].first,
      Lef[Rig[P].first].first = Lef[P].first;
  }
  for (int i = (2); i <= (m); i++) {
    int j = Fail[i - 1];
    for (; j > 0 && Check(i, j, Match); j = Fail[j])
      ;
    Fail[i] = Check(i, j, Match) ? 0 : (j + 1);
  }
  int K = 0, ans = 0;
  for (int i = (1); i <= (n); i++) {
    for (; K != 0 && Check(i, K, Pattern); K = Fail[K])
      ;
    K = Check(i, K, Pattern) ? 0 : (K + 1);
    if (K == m) ans++, K = Fail[K];
  }
  cout << ans << endl;
  return 0;
}
