module mod_segment_tree
  implicit none
  integer, private, parameter :: intkind = 4
  type segment_tree
    integer(intkind) :: default = 0
    integer :: n, p
    integer(intkind), private, allocatable :: body(:)
  contains
    procedure :: init => init
    procedure :: update => update
    procedure :: query => query
  end type
  interface segment_tree
    module procedure :: newst0
  end interface
contains
  integer(intkind) function op(x, y) result(res)
    integer(intkind), intent(in) :: x, y
    res = max(x, y)
  end
  type(segment_tree) function newst0(n, def) result(res)
    integer, intent(in) :: n
    integer(intkind), optional, intent(in) :: def
    integer :: p
    p = 1
    do while (p < n)
      p = lshift(p, 1)
    end do
    if (present(def)) res%default = def
    res%n = n
    res%p = p
    allocate(res%body(2 * p - 1))
    res%body = res%default
  end
  subroutine init(this, n, def)
    class(segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer(intkind), optional, intent(in) :: def
    integer :: p
    p = 1
    do while (p < n)
      p = lshift(p, 1)
    end do
    if (present(def)) this%default = def
    this%n = n
    this%p = p
    allocate(this%body(2 * p - 1))
    this%body = this%default
  end
  subroutine update(this, i, v)
    class(segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer(intkind), optional, intent(in) :: v
    integer :: x
    x = i + this%p - 1
    this%body(x) = merge(v, this%default, present(v))
    do while (x > 1)
      x = x / 2
      this%body(x) = op(this%body(2 * x), this%body(2 * x + 1))
    end do
  end
  integer(intkind) function query(this, a, b) result(res)
    class(segment_tree), intent(in) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    res = this%default
    l = a + this%p - 1
    r = b + this%p - 1
    do while (l <= r)
      if (mod(l, 2) == 1) then
        res = op(res, this%body(l))
        l = l + 1
      end if
      if (mod(r, 2) == 0) then
        res = op(res, this%body(r))
        r = r - 1
      end if
      l = l / 2
      r = r / 2
    end do
  end
end module mod_segment_tree
program flat_subsequence
  use mod_segment_tree
  implicit none
  integer :: n, k, a(300000) = 0, p, q, m, l, r, i
  type(segment_tree) :: st
  read(*,*) n, k
  read(*,*) (a(i), i = 1, n)
  p = minval(a(1:n))
  if (p == 0) then
    a(1:n) = a(1:n) + 1
    p = p + 1
  end if
  q = maxval(a(1:n))
  st = segment_tree(q)
  do i = 1, n
    l = max(p, a(i) - k)
    r = min(q, a(i) + k)
    m = st%query(l, r) + 1
    call st%update(a(i), m)
  end do
  write(*,'(i0)') st%query(p, q)
end program flat_subsequence