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

dp = Array.new(N + 1, Float::INFINITY)
dp[1] = 0

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

puts dp[N]
