PROGRAM ATCODER

implicit none
integer :: n, k, i, j
integer, allocatable :: h(:), dp(:)

read(*,*) n, k
allocate(h(n), dp(n))
read(*,*) (h(i), i=1,n)

dp = 10**9
dp(1) = 0

do i = 2, n
	do j = 1, min(k,i-1)
		call chmin(dp(i), dp(i-j)+abs(h(i)-h(i-j)))
    end do
end do

write(*,'(i0)') dp(n)

END PROGRAM ATCODER

subroutine chmin(dp,x)
implicit none
integer :: dp, x

if (dp > x) then
	dp = x
	return
end if

end subroutine