# include <bits/stdc++.h>
# define sz(x) (int)((x).size())
# define f first 
# define s second
# define pb push_back
# define ub upper_bound
# define lb lower_bound
# define all(x) x.begin(), x.end()
# define pqueue priority_queue
# define Speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
# define rep(i,n) for(int i=0;i<n;++i)
# define forn(i,n) for(int i=1;i<=n;++i)

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

# define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
# define debug cerr<<"ERROR"<<endl
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

template<class T> void umin(T &a,T b){a=min(a,b);}
template<class T> void umax(T &a,T b){a=max(a,b);}

typedef long long ll;
typedef pair<int,int> pii;

const int inf=1e9,mod=998244353,N=2e5+5;

int n,k,ans,l,r,cur;

int longest_subseq(int n, int k, vector<int> s) 
{ 
  
    // Creating a list with 
    // all 0's of size 
    // equal to the length of string 
    vector<int> dp(n, 0); 
  
    // Supporting list with 
    // all 0's of size 26 since 
    // the given string consists 
    // of only lower case alphabets 
    int max_length[n] = {0}; 
  
    for (int i = 0; i < n; i++)  
    { 
  
        // Converting the ascii value to 
        // list indices 
        int curr = s[i] ; 
          
        // Determining the lower bound 
        int lower = max(0, curr - k); 
          
        // Determining the upper bound 
        int upper = min(n-1, curr + k); 
          
        // Filling the dp array with values 
        for (int j = lower; j < upper + 1; j++) 
        { 
            dp[i] = max(dp[i], max_length[j] + 1); 
        } 
        //Filling the max_length array with max 
        //length of subsequence till now 
        max_length[curr] = max(dp[i], max_length[curr]); 
    } 
  
    int ans = 0; 
  
    for(int i:dp) ans = max(i, ans); 
  
    // return the max length of subsequence 
    return ans; 
} 

void solve(){
	cin>>n>>k;
	vector<int>a(n);
	rep(i,n){
		cin>>a[i];
	}
	cout<<longest_subseq(n,k,a);
    

}

int main(){
 	Speed();
	int T=1; 
	//cin>>T;	
	while(T--)solve();
}