#include <bits/stdc++.h>
using namespace std;

#define endl "\\n"
#define LL long long
#define ULL unsigned long long
const LL INF = 1LL << 59;
const double PI = 2.0 * acos(0.0);

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//// Including Policy Based DS
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//
// typedef tree<
// int,
// null_type,
// less< int >,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#define BOOST                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define sf scanf
#define pf printf
#define F first
#define S second
#define pb push_back
#define NL pf("\\n")
#define ALL(v) v.begin(), v.end()
#define si(x) sf("%d", &x);
#define sii(x, y) sf("%d%d", &x, &y);
#define siii(x, y, z) sf("%d%d%d", &x, &y, &z);
#define sl(x) sf("%lld", &x);
#define sll(x, y) sf("%lld%lld", &x, &y);
#define slll(x, y, z) sf("%lld%lld%lld", &x, &y, &z);
#define sqr(x) ((x) * (x))
#define mem(x, y) memset(x, y, sizeof x)
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n")
#define deb(x) cerr << #x << " :: " << x << "\\n"
#define SV(v) sort(v.begin(), v.end())
#define SVR(v, cmp) sort(v.begin(), v.end(), cmp)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define cnd tree[idx]
#define lc tree[idx * 2]
#define rc tree[idx * 2 + 1]
#define lnd (2 * idx), (b), ((b + e) / 2)
#define rnd ((2 * idx) + 1), (((b + e) / 2) + 1), (e)

// graph direction array [4]
int X[4] = {0, 0, -1, 1};
int Y[4] = {1, -1, 0, 0};
// graph direction array [8]
// int X[8]={0,0,1,-1,-1,1,1,-1};
// int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
// int BX[8]={0,0,1,-1,-1,1,1,-1};
// int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
// int KX[8] = {1,1,2,2,-1,-1,-2,-2};
// int KY[8] = {2,-2,1,-1,2,-2,1,-1};

// Bit Operations
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
inline bool checkBit(int N, int pos) {
  return (bool)(N & (1 << pos));
} // check bit
inline int setBit(int N, int pos) { return N = N | (1 << pos); } // set bit
inline int unsetBit(int N, int pos) {
  return N = (N & (~(1 << pos)));
} // unset bit
inline int toggleBit(int N, int pos) {
  return N = (N ^ (1 << pos));
} // toggle bit

inline LL modMul(LL a, LL b, LL mod) {
  LL ans = 0;
  a = a % mod;
  while (b > 0) {
    if (b % 2)
      ans = (ans % mod + a % mod) % mod;
    a = (a % mod * 2 % mod) % mod;
    b /= 2;
  }
  return ans % mod;
}
inline LL powerMod(LL a, LL b, LL mod) {
  if (b == 0)
    return 1LL % mod;
  LL x = powerMod(a, b / 2, mod);
  x = (x % mod * x % mod) % mod;
  if (b % 2 == 1)
    x = (x % mod * a % mod) % mod;
  return x % mod;
}
inline LL lcm(LL a, LL b) {
  LL g = __gcd(a, b);
  return ((a / g) * b);
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 3e5 + 50;

int tree[mx];
void update(int idx, int b, int e, int pos, int v) {
  if (b == e && b == pos) {
    cnd = max(cnd, v);
    return;
  }
  int mid = (b + e) >> 1;
  if (pos <= mid)
    update(lnd, pos, v);
  else
    update(rnd, pos, v);
  cnd = max(lc, rc);
}
int query(int idx, int b, int e, int l, int r) {
  if (l > e || r < b)
    return 0;
  if (l <= b && r >= e)
    return cnd;
  return max(query(lnd, l, r), query(rnd, l, r));
}

int arr[mx];
int ans[mx];

int main() {
  BOOST;
#ifdef MujahidPC
  double start = clock();
  //        READ;
  //        WRITE;
#endif
  int t, n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 1; i <= n; i++)
    arr[i]++;
  const int M = 300002;
  int res = 0;
  for (int i = n; i > 0; i--) {
    int x = arr[i];
    int l = max(1, arr[i] - m);
    int r = min(M, arr[i] + m);
    int d = query(1, 1, M, l, r);
    ans[i] = d + 1;
    update(1, 1, M, x, ans[i]);
    res = max(res, ans[i]);
  }

  cout << res << endl;

#ifdef MujahidPC
  double tim = (clock() - start) / CLOCKS_PER_SEC;
  cerr << "Running Time : " << tim << " \\n";
#endif
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define endl "\\n"
#define LL long long
#define ULL unsigned long long
const LL INF = 1LL << 59;
const double PI = 2.0 * acos(0.0);

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;

//// Including Policy Based DS
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//
// typedef tree<
// int,
// null_type,
// less< int >,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;

#define READ freopen("in.txt", "r", stdin)
#define WRITE freopen("out.txt", "w", stdout)
#define BOOST                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define sf scanf
#define pf printf
#define F first
#define S second
#define pb push_back
#define NL pf("\\n")
#define ALL(v) v.begin(), v.end()
#define si(x) sf("%d", &x);
#define sii(x, y) sf("%d%d", &x, &y);
#define siii(x, y, z) sf("%d%d%d", &x, &y, &z);
#define sl(x) sf("%lld", &x);
#define sll(x, y) sf("%lld%lld", &x, &y);
#define slll(x, y, z) sf("%lld%lld%lld", &x, &y, &z);
#define sqr(x) ((x) * (x))
#define mem(x, y) memset(x, y, sizeof x)
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\\n")
#define deb(x) cerr << #x << " :: " << x << "\\n"
#define SV(v) sort(v.begin(), v.end())
#define SVR(v, cmp) sort(v.begin(), v.end(), cmp)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define cnd tree[idx]
#define lc tree[idx * 2]
#define rc tree[idx * 2 + 1]
#define lnd (2 * idx), (b), ((b + e) / 2)
#define rnd ((2 * idx) + 1), (((b + e) / 2) + 1), (e)

// graph direction array [4]
int X[4] = {0, 0, -1, 1};
int Y[4] = {1, -1, 0, 0};
// graph direction array [8]
// int X[8]={0,0,1,-1,-1,1,1,-1};
// int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
// int BX[8]={0,0,1,-1,-1,1,1,-1};
// int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
// int KX[8] = {1,1,2,2,-1,-1,-2,-2};
// int KY[8] = {2,-2,1,-1,2,-2,1,-1};

// Bit Operations
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
inline bool checkBit(int N, int pos) {
  return (bool)(N & (1 << pos));
} // check bit
inline int setBit(int N, int pos) { return N = N | (1 << pos); } // set bit
inline int unsetBit(int N, int pos) {
  return N = (N & (~(1 << pos)));
} // unset bit
inline int toggleBit(int N, int pos) {
  return N = (N ^ (1 << pos));
} // toggle bit

inline LL modMul(LL a, LL b, LL mod) {
  LL ans = 0;
  a = a % mod;
  while (b > 0) {
    if (b % 2)
      ans = (ans % mod + a % mod) % mod;
    a = (a % mod * 2 % mod) % mod;
    b /= 2;
  }
  return ans % mod;
}
inline LL powerMod(LL a, LL b, LL mod) {
  if (b == 0)
    return 1LL % mod;
  LL x = powerMod(a, b / 2, mod);
  x = (x % mod * x % mod) % mod;
  if (b % 2 == 1)
    x = (x % mod * a % mod) % mod;
  return x % mod;
}
inline LL lcm(LL a, LL b) {
  LL g = __gcd(a, b);
  return ((a / g) * b);
}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 3e5 + 50;

int tree[4 * mx];
void update(int idx, int b, int e, int pos, int v) {
  if (b == e && b == pos) {
    cnd = max(cnd, v);
    return;
  }
  int mid = (b + e) >> 1;
  if (pos <= mid)
    update(lnd, pos, v);
  else
    update(rnd, pos, v);
  cnd = max(lc, rc);
}
int query(int idx, int b, int e, int l, int r) {
  if (l > e || r < b)
    return 0;
  if (l <= b && r >= e)
    return cnd;
  return max(query(lnd, l, r), query(rnd, l, r));
}

int arr[mx];
int ans[mx];

int main() {
  BOOST;
#ifdef MujahidPC
  double start = clock();
  //        READ;
  //        WRITE;
#endif
  int t, n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int i = 1; i <= n; i++)
    arr[i]++;
  const int M = 300002;
  int res = 0;
  for (int i = n; i > 0; i--) {
    int x = arr[i];
    int l = max(1, arr[i] - m);
    int r = min(M, arr[i] + m);
    int d = query(1, 1, M, l, r);
    ans[i] = d + 1;
    update(1, 1, M, x, ans[i]);
    res = max(res, ans[i]);
  }

  cout << res << endl;

#ifdef MujahidPC
  double tim = (clock() - start) / CLOCKS_PER_SEC;
  cerr << "Running Time : " << tim << " \\n";
#endif
  return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) REP(j, i) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\\n';
  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) for (int j = max(0, i - 100); j < i; j++) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\\n';
  return 0;
}


// #define _GLIBCXX_DEBUG
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1000000000;

int main() {
  IOS;
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> dp(N);
  int ans = 0;

  for (int i = 0; i < N; i++) {
    chmax(dp[i], 1);
    for (int j = 0; j < i; j++) {

      if (abs(A[j] - A[i]) <= K)
        chmax(dp[i], dp[j] + 1);
      chmax(ans, dp[i]);
    }
  }
  cout << dp[N - 1] << endl;
}

#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

int op(int a, int b) { return max(a, b); }

int e() { return (int)-1e9; }
int main() {
  int am = 300100;
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  segtree<int, op, e> seg(am);

  for (int i = 0; i < n; i++) {

    int ai = A[i];
    int next = seg.prod(max(0, ai - k), min(am - 1, ai + k + 1)) + 1;
    seg.set(ai, max(next, 1));
  }

  cout << seg.prod(0, am) << endl;
}