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

def chmin(min,x)
  if min.nil? or min>x
    return x
  else
    return min
  end
end
1.upto(n){|i|
  min=nil
  [1,i-k].max.upto(i){|j| 
  min=chmin(min, (h[i]-h[j]).abs+d[j])
  d[i]=min
  }}
puts d[n]