#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
const ll INF = numeric_limits<ll>::max();
const int inf = numeric_limits<int>::max();
const char nl = '\n', sp = ' ';
int main(){
//solution notes
/*
*/
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n, x;
cin >> n >> x;
vector<ll> arr(n,0);
for(int i = 0; i < n; i++){
cin >> arr[i];
}
vector<ll> LIS(n,0); //longest IS form [O,i]
vector<ll> LDS(n,0);
vector<ll> OPT(n,INF); // optimal of length n
vector<ll> BACKOPT(n,-INF);
for(int i = 0; i < n; i++){
ll value = arr[i];
auto it = lower_bound(OPT.begin(), OPT.end(), value);
int index = it - OPT.begin() ;
OPT[index] = value;
LIS[i] = index+1;
//longest increasing from 0 to i, that has the minimum ending element as established already
}
for(int i = n-1; i >= 0; i--){
//find longest decreasing sequence
ll value = arr[i];
auto it = lower_bound(BACKOPT.begin(), BACKOPT.end(), value, greater<ll>());
//find first element LEQ, we want to replace this
int index = it - BACKOPT.begin() ;
BACKOPT[index] = value;
LDS[i] = index+1; //longest with lowest last element
}
ll maxi = -1;
for(int i = 0; i < n; i++){
maxi = max(maxi,LIS[i]);
}
OPT.clear();
OPT.resize(n,INF);
for(int i = 0; i < n; i++){
ll val = arr[i];
ll lo = val-x+1;
ll hi = val+x-1;
// return 0;
auto loit = lower_bound(OPT.begin(),OPT.end(),lo);
auto hiit = upper_bound(OPT.begin(),OPT.end(),hi);
int L = loit-OPT.begin();
int R = hiit-OPT.begin();
if(L < R){
maxi = max(maxi, LDS[i] + R);
}
ll value = arr[i];
auto it = lower_bound(OPT.begin(), OPT.end(), value);
int index = it - OPT.begin() ;
OPT[index] = value;
}
cout << maxi << endl;
}