#include <bits/stdc++.h>
using namespace std;
long long tree[200005], TREE[200005], step[200005], NOW, now, F[200005], H;
int a[200005], b[200005], A[200005], B[200005], n, m, i, j, X, ans, Y,
    L[200005];
int bit(int k) { return k & -k; }
const int MOD = 1000000007;
long long quick(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1ll * ans * a % MOD;
    a = 1ll * a * a % MOD;
    b /= 2;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= m; i++) scanf("%d", &b[i]);
  for (i = 1; i <= m; i++) A[a[i]] = i, B[b[i]] = i;
  if (n == 1) {
    cout << m;
    return 0;
  }
  step[0] = 1;
  for (i = 1; i <= m; i++) step[i] = step[i - 1] * 200003 % MOD;
  for (i = 1; i <= n; i++) now = (now + step[n - i] * A[i]) % MOD;
  for (i = 1; i <= n; i++)
    for (j = B[i]; j <= m; j += bit(j)) TREE[j]++;
  for (i = 1; i <= n; i++) {
    X = 0;
    for (j = B[i]; j; j -= bit(j)) X += TREE[j];
    NOW = (NOW + step[n - i] * X) % MOD;
    L[i] = X;
    for (j = B[i]; j <= m; j += bit(j)) tree[j] = (tree[j] + step[n - i]) % MOD;
    F[i] = step[n - i];
  }
  if (NOW == now) ans++;
  H = 1;
  for (i = n + 1; i <= m; i++) {
    X = 0;
    for (j = B[i - n]; j; j -= bit(j)) X += TREE[j];
    NOW = (NOW - step[n - 1] * X % MOD * H % MOD + MOD) % MOD;
    for (j = B[i - n]; j <= m; j += bit(j)) TREE[j]--;
    for (j = m; j; j -= bit(j)) NOW = (NOW - tree[j] + MOD) % MOD;
    for (j = B[i - n]; j; j -= bit(j)) NOW = (NOW + tree[j]) % MOD;
    for (j = B[i - n]; j <= m; j += bit(j))
      tree[j] = (tree[j] - F[i - n] + MOD) % MOD;
    for (j = B[i]; j; j -= bit(j)) NOW = (NOW - tree[j] + MOD) % MOD;
    for (j = m; j; j -= bit(j)) NOW = (NOW + tree[j]) % MOD;
    Y++;
    H = H * quick(step[1], MOD - 2) % MOD;
    X = 0;
    for (j = B[i]; j <= m; j += bit(j)) TREE[j]++;
    for (j = B[i]; j; j -= bit(j)) X += TREE[j];
    NOW = (NOW + H * X) % MOD;
    L[i] = X;
    for (j = B[i]; j <= m; j += bit(j)) tree[j] = (tree[j] + H) % MOD;
    if (NOW * step[Y] % MOD == now) ans++;
    F[i] = H;
  }
  cout << ans;
  return 0;
}
