n, k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)

c = [0, (h[0] - h[1]).abs]
(2..(n - 1)).each do |i|
  x = []
  (([0, i - k].max)..(i - 1)).each do |j|
    x << (c[j] + (h[j] - h[i]).abs)
  end
  c[i] = x.min
end

puts c[-1]
