program bbb

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

read*, n, k
allocate(h(n))
allocate(dp(-k+1:n))
read*, h

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

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

end program