#line 1 "d.cpp"
#include <algorithm>
#include <numeric>
#include <vector>

#line 1 "~/git/library/utility/io.cpp"



/**
 * @brief 入出力
 * @author えびちゃん
 */

#include <cstddef>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <tuple>
#include <utility>

class format {
public:
  using size_type = size_t;

private:
  char const* M_fmt;
  size_type M_nvars = 0;
  size_type M_error = -1;

  void M_print(std::ostream& os, char const* pos) const {
    while (*pos) {
      if (*pos == '{' || *pos == '}') ++pos;
      os << *pos++;
    }
  }

  template <typename Tp, typename... Args>
  void M_print(std::ostream& os, char const* pos, Tp const& x, Args const&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          ++pos;
          os << '{';
        } else {
          char const* next = M_print_formatted(os, pos, x);
          return M_print(os, next, xs...);
        }
      } else {
        if (*pos == '}') ++pos;
        os << *pos;
      }
      ++pos;
    }
    char const* next = M_print_formatted(os, pos, x);
    M_print(os, next, xs...);
  }

  template <typename Tp>
  char const* M_print_formatted(std::ostream& os, char const* pos, Tp const& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    os << x;
    return ++pos;
  }

  void M_scan(std::istream& is, char const* pos) const {
    while (true) {
      if (*pos == '{' || *pos == '}') ++pos;
      if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
      } else {
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp, typename... Args>
  void M_scan(std::istream& is, char const* pos, Tp& x, Args&&... xs) const {
    while (true) {
      if (*pos == '{') {
        if (pos[1] == '{') {
          if (is.peek() == '{') {
            ++pos;
            is.get();
          } else {
            return;
          }
        } else {
          char const* next = M_scan_formatted(is, pos, x);
          return M_scan(is, next, xs...);
        }
      } else if (isspace(*pos)) {
        while (isspace(is.peek())) is.get();
        ++pos;
      } else {
        if (*pos == '}') ++pos;
        if (is.peek() == *pos) {
          ++pos;
          is.get();
        } else {
          return;
        }
      }
    }
  }

  template <typename Tp>
  char const* M_scan_formatted(std::istream& is, char const* pos, Tp& x) const {
    // parse flags, preferably
    while (*++pos != '}') {}
    is >> x;
    return ++pos;
  }

public:
  constexpr format(char const* fmt): M_fmt(fmt) {
    bool opens = 0;
    size_type i = 0;
    for (; fmt[i]; ++i) {
      if (fmt[i] == '{') {
        if (fmt[i+1] == '{') { ++i; continue; }  // escaped
        if (opens) { M_error = i; return; }
        opens = true;
      } else if (fmt[i] == '}') {
        if (fmt[i+1] == '}') { ++i; continue; }  // escaped
        if (!opens) { M_error = i; return; }
        opens = false;
        ++M_nvars;
      }
    }
    if (opens) { M_error = i; return; }
  }

  template <typename... Args>
  void print_(std::ostream& os, Args const&... xs) const {
    M_print(os, M_fmt, xs...);
  }

  template <typename... Args>
  void scan_(std::istream& is, Args&&... xs) const {
    M_scan(is, M_fmt, xs...);
  }

  constexpr size_type count() const { return M_nvars; }
  constexpr size_type error() const { return M_error; }
};

#define VA_COUNT(...)                                           \
  std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value

#define fprint(os, fmt, ...) (void)({                           \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.print_(os, ##__VA_ARGS__);                           \
    })

#define fprintln(os, fmt, ...) (void)({         \
      fprint(os, fmt, ##__VA_ARGS__);           \
      os << '\n';;                              \
    })

#define print(...) fprint(std::cout, ##__VA_ARGS__)
#define println(...) fprintln(std::cout, ##__VA_ARGS__)
#define eprint(...) fprint(std::cerr, ##__VA_ARGS__)
#define eprintln(...) fprintln(std::cerr, ##__VA_ARGS__)

#define fscan(is, fmt, ...) (void)({                            \
      constexpr format fmt_(fmt);                               \
      constexpr size_t lhs = fmt_.count();                      \
      constexpr size_t rhs = VA_COUNT(__VA_ARGS__);             \
      static_assert(lhs == rhs, "size mismatch");               \
      static_assert(fmt_.error()+1 == 0, "misformatted");       \
      fmt_.scan_(is, ##__VA_ARGS__);                            \
    })

#define scan(...) fscan(std::cin, __VA_ARGS__)

__attribute__((constructor))
void ioinit() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cerr.tie(nullptr);
  std::cout << std::fixed << std::setprecision(16);
}


#line 6 "d.cpp"

#include <atcoder/segtree>

namespace min {
  int op(int x, int y) { return std::min(x, y); }
  int e() { return 1e9; }
}

namespace max {
  int op(int x, int y) { return std::max(x, y); }
  int e() { return -1e9; }
}

// /////////////////////////////////////////////////////////////
template <class Tp>
struct Edge {
  size_t dst;
  Tp cap, cost;
  size_t rev;
  Edge(size_t dst, Tp cap, Tp cost, size_t rev):
    dst(dst), cap(cap), cost(cost), rev(rev)
  {}
};

template <class Tp>
struct Graph: public std::vector<std::vector<Edge<Tp>>> {
  Graph(size_t n): std::vector<std::vector<Edge<Tp>>>(n) {}

  void connect_to(size_t src, size_t dst, Tp cost) {
    (*this)[src].emplace_back(dst, 1, cost, (*this)[dst].size());
    (*this)[dst].emplace_back(src, 0, -cost, (*this)[src].size()-1);
  }
};

template <class Tp>
Tp inf() { return 1 << 29; }

template <class Tp>
Tp mincost_flow(Graph<Tp>& g, size_t s, size_t t, Tp f) {
  Tp res = 0;
  Tp max = inf<Tp>();
  while (f > 0) {
    std::vector<Tp> dist(g.size(), inf<Tp>());
    std::vector<size_t> pv(g.size()), pe(g.size());
    dist[s] = 0;
    bool updated = true;
    while (updated) {
      updated = false;
      for (size_t v=0; v<g.size(); ++v) {
        if (dist[v] == max) continue;

        for (size_t i=0; i<g[v].size(); ++i) {
          Edge<Tp>& e = g[v][i];
          if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
            dist[e.dst] = dist[v] + e.cost;
            pv[e.dst] = v;
            pe[e.dst] = i;
            updated = true;
          }
        }
      }
    }

    if (dist[t] == max) return res;
    Tp d = f;
    for (size_t v=t; v!=s; v=pv[v])
      d = std::min(d, g[pv[v]][pe[v]].cap);

    f -= d;
    res += d * dist[t];
    for (size_t v=t; v!=s; v=pv[v]) {
      Edge<Tp>& e = g[pv[v]][pe[v]];
      e.cap -= d;
      g[v][e.rev].cap += d;
    }
  }
  return res;
}
// /////////////////////////////////////////////////////////////

int main() {
  size_t n;
  int k;
  scan("{} {}", n, k);

  std::vector<int> a(n);
  for (auto& ai: a) scan("{}", ai);

  atcoder::segtree<int, min::op, min::e> minst(n);
  atcoder::segtree<int, max::op, max::e> maxst(n);

  size_t source = n;
  size_t sink = source + 1;
  Graph<int> mcf(sink+1);

  std::vector<size_t> ii(n);
  std::iota(ii.begin(), ii.end(), 0);
  std::sort(ii.begin(), ii.end(), [&](size_t i, size_t j) {
    return (a[i] != a[j])? a[i] < a[j]: j < i;
  });

  for (auto i: ii) {
    size_t ir = maxst.max_right(i, [&](int acc) { return acc < a[i] - k; });
    if (ir < n) mcf.connect_to(i, ir, -1);
    maxst.set(i, a[i]);
  }

  std::sort(ii.begin(), ii.end(), [&](size_t i, size_t j) {
    return (a[i] != a[j])? a[j] < a[i]: j < i;
  });

  for (auto i: ii) {
    size_t ir = minst.max_right(i, [&](int acc) { return acc > a[i] + k; });
    if (ir < n) mcf.connect_to(i, ir, -1);
    minst.set(i, a[i]);
  }

  for (size_t i = 0; i+1 < n; ++i) {
    mcf.connect_to(i, i+1, 0);
  }

  for (size_t i = 0; i < n; ++i) {
    mcf.connect_to(i, sink, -1);
  }

  mcf.connect_to(source, 0, 0);

  auto res = -mincost_flow(mcf, source, sink, int(n));
  println("{}", res);
}
