#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long INF = 1e12;
const int MOD = 1e9 + 7;
const unsigned long long BIT_FLAG_0 = (1 << 0);
const unsigned long long BIT_FLAG_1 = (1 << 1);
const unsigned long long BIT_FLAG_2 = (1 << 2);
const unsigned long long BIT_FLAG_3 = (1 << 3);
const unsigned long long BIT_FLAG_4 = (1 << 4);
const unsigned long long BIT_FLAG_5 = (1 << 5);
const unsigned long long BIT_FLAG_6 = (1 << 6);
const unsigned long long BIT_FLAG_7 = (1 << 7);
const long long dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const long long Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
                Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
void print() { cout << "\n"; }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail) != 0) cout << " ";
  print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) cout << " ";
  }
  cout << "\n";
}
template <class T>
void print(set<T> &set) {
  for (auto &a : set) {
    cout << a << " ";
  }
  cout << "\n";
}
template <class T>
void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}
long long power(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1) ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
}
long long comb(long long n, long long k) {
  vector<vector<long long>> v(n + 1, vector<long long>(n + 1, 0));
  for (long long i = 0; i < (long long)((v).size()); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (long long k = 1; k < (long long)((v).size()); k++) {
    for (long long j = 1; j < k; j++) {
      v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
    }
  }
  return v[n][k];
}
void add(long long &a, long long b) {
  a += b;
  if (a >= MOD) a -= MOD;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
__attribute__((constructor)) void faster_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
}
template <class Tp>
class starry_sky_tree {
  std::vector<Tp> c;
  size_t roundup_powtwo(size_t n) const {
    if (n == 0) return 1;
    if ((n & (n - 1)) == 0) return n + 1;
    return size_t(1) << (64 - __builtin_clzll(n));
  }
  void init_build() {
    size_t m = c.size();
    for (size_t i = m / 2; i-- > 1;) {
      Tp max = std::max(c[i << 1], c[i << 1 | 1]);
      c[i << 1] -= max;
      c[i << 1 | 1] -= max;
      c[i] = max;
    }
  }
  void maintain(size_t i) {
    Tp max = std::max(c[i << 1], c[i << 1 | 1]);
    c[i << 1] -= max;
    c[i << 1 | 1] -= max;
    c[i] += max;
  }

 public:
  starry_sky_tree() {}
  starry_sky_tree(size_t n, const Tp &e = Tp()) {
    size_t m = 2 * roundup_powtwo(n);
    c.assign(m, e - e);
    c[1] = e;
  }
  template <class ForwardIt>
  starry_sky_tree(ForwardIt first, ForwardIt last) {
    size_t m = 2 * roundup_powtwo(std::distance(first, last));
    c.assign(m, *first - *first);
    for (size_t i = m / 2; i < m; ++i) {
      c[i] = *first++;
      if (first == last) break;
    }
    init_build();
  }
  void add(size_t l, size_t r, const Tp &x) {
    size_t m = c.size();
    l += m / 2;
    r += m / 2;
    while (l < r) {
      if (l & 1) {
        c[l] += x;
        ++l;
      }
      if (r & 1) {
        --r;
        c[r] += x;
      }
      if ((l - 1) > 1) maintain((l - 1) >> 1);
      if (r < m) maintain(r >> 1);
      l >>= 1;
      r >>= 1;
    }
    for (size_t i = l - 1; i > 1; i >>= 1) maintain(i);
    assert(r < m);
    for (size_t i = r; i >= 1; i >>= 1) maintain(i);
  }
  Tp max(size_t l, size_t r) const {
    assert(l < r);
    size_t m = c.size();
    l += m / 2;
    r += m / 2;
    std::vector<size_t> ll, rr;
    size_t li = l;
    size_t ri = r;
    while (li < ri) {
      if (li & 1) ll.push_back(li++);
      if (ri & 1) rr.push_back(--ri);
      li >>= 1;
      ri >>= 1;
    }
    std::vector<Tp> cand;
    Tp curl = c[1];
    for (size_t i = li - 1; i > 1; i >>= 1) curl += c[i];
    for (size_t i = li - 1; !ll.empty();) {
      size_t j = ll.back();
      (i <<= 1) |= 1;
      if (i == j) {
        cand.push_back(curl + c[i--]);
        ll.pop_back();
      }
      curl += c[i];
    }
    Tp curr = c[1];
    for (size_t i = ri; i > 1; i >>= 1) curr += c[i];
    for (size_t i = ri; !rr.empty();) {
      size_t j = rr.back();
      i <<= 1;
      if (i == j) {
        cand.push_back(curr + c[i++]);
        rr.pop_back();
      }
      curr += c[i];
    }
    return *std::max_element(cand.begin(), cand.end());
  }
};
signed main() {
  long long n, k;
  cin >> n >> k;
  starry_sky_tree<long long> sst(300010);
  vector<long long> A(n);
  long long m = 0;
  for (long long ni = 0; ni < (long long)(n); ni++) {
    long long v;
    cin >> v;
    A[ni] = v;
    chmax(m, v);
  }
  for (long long ni = 0; ni < (long long)(n); ni++) {
    long long a = A[ni];
    long long l = max((long long)1, a - k);
    long long r = min(m, a + k);
    long long Mv = sst.max(l, r + 1);
    long long now = sst.max(a, a + 1);
    long long diff = Mv + 1 - now;
    sst.add(a, a + 1, diff);
  }
  long long ans = sst.max(1, m + 1);
  print(ans);
}
