N, K = gets.split.map(&:to_i)
H = [Float::INFINITY] + gets.split.map(&:to_i)

dp = Array.new(N + 1)
dp[1] = 0

2.upto(N) do |i|
  min = Float::INFINITY

  (([1, i - K].max)..(i - 1)).each do |j|
    cost = dp[j] + (H[j] - H[i]).abs
    min = [min, cost].min
  end

  dp[i] = min
end

puts dp[N]
