#include <bits/stdc++.h>
using namespace std;
struct Seg {
  vector<long long> sum;
  vector<long long> mx;
  Seg() {
    sum.resize(1 << 19, 0);
    mx.resize(1 << 19, 0);
  }
  void set_val(long long l, long long r, long long val, long long bottom = 0,
               long long top = 1 << 18, long long pos = 1) {}
  void add_val(long long l, long long r, long long val, long long bottom = 0,
               long long top = 1 << 18, long long pos = 1) {
    if (l <= bottom && top <= r) {
      sum[pos] += val;
      return;
    }
    if (r <= bottom || l >= top) return;
    long long mid = (bottom + top) / 2;
    add_val(l, r, val, bottom, mid, pos * 2);
    add_val(l, r, val, mid, top, pos * 2 + 1);
    mx[pos] = max<long long>(mx[pos * 2] + sum[pos * 2],
                             mx[pos * 2 + 1] + sum[pos * 2 + 1]);
  }
  long long max_val(long long l, long long r, long long bottom = 0,
                    long long top = 1 << 18, long long pos = 1) {
    if (l <= bottom && top <= r) return sum[pos] + mx[pos];
    if (r <= bottom || l >= top) return -1000000000000000LL;
    long long mid = (bottom + top) / 2;
    return max<long long>(max_val(l, r, bottom, mid, pos * 2),
                          max_val(l, r, mid, top, pos * 2 + 1)) +
           sum[pos];
  }
};
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < (n); i++) cin >> a[i];
  Seg seg;
  int result = 0;
  for (int i = 0; i < (n); i++) {
    int l = max(0, a[i] - k);
    int r = a[i] + k + 1;
    long long mx = seg.max_val(l, r);
    long long cv = seg.max_val(a[i], a[i] + 1);
    seg.add_val(a[i], a[i] + 1, mx + 1 - cv);
  }
  cout << seg.max_val(1, 330000);
  return 0;
}
