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

dp = Array.new(n, 10 ** 9)
dp[0] = 0
dp[1] = (h[0] - h[1]).abs

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

puts dp[-1]
