#include <bits/stdc++.h>
using namespace std;
long long s[300005], dp[300005];
vector<long long> V[300005];
signed main() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) cin >> s[i];
  for (long long i = 1; i <= n; i++) {
    dp[i] = 1;
    for (long long j = 0; j < V[s[i]].size(); j++) {
      dp[i] = max(dp[i], V[s[i]][j] + 1);
    }
    for (long long j = -k; j <= k; j++) {
      if (s[i] + j >= 0 && s[i] + j <= n) V[s[i] + j].push_back(dp[i]);
    }
  }
  long long maxi = -1;
  for (long long i = 1; i <= n; i++) maxi = max(maxi, dp[i]);
  cout << maxi << endl;
  return 0;
}
