program main
  implicit none
  integer :: i
  integer,allocatable,dimension(:) :: c, h
  integer :: N, K
  read(*, *), N, K
  allocate(h(N), c(N))
  read(*, *), h
  c(1) = 0
  c(2) = abs(h(1) - h(2))
  do i = 3, N
    c(i) = minval([(c(i-k)+abs(h(i-k)-h(i)), k=1,min(i,K))])
  end do
  print*,(c(N))
end program