n,k=gets.split.map(&:to_i)
h=gets.split.map(&:to_i)
d=Array.new(n+1,0)
h.unshift(0)

1.upto(n){|i|
  min=1000000000
  [1,i-k].max.upto(i){|j| 
    x=(h[i]-h[j]).abs+d[j]
    min=(min>x)?x:min
    d[i]=min
  }}
puts d[n]