#include <bits/stdc++.h>
using namespace std;
int ANS(int n, int k, vector<int>& ar) {
  vector<int> dp(n, 0);
  int max_length[10] = {0};
  for (int i = 0; i < n; i++) {
    int l = max(0, ar[i] - k);
    int r = min(9, ar[i] + k);
    for (int j = l; j < r + 1; j++) {
      dp[i] = max(dp[i], max_length[j] + 1);
    }
    max_length[ar[i]] = max(dp[i], max_length[ar[i]]);
  }
  int ans = 0;
  for (auto i : dp) ans = max(i, ans);
  return ans;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> ar(n + 1);
  for (int i = 0; i < n; i++) cin >> ar[i];
  cout << ANS(n, k, ar) << "\n";
  return 0;
}
