n, k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)
dp = Array.new(n, Float::INFINITY)
dp[0] = 0
n.times do |i|
  1.upto(k) do |j|
    break if i + j > n - 1
    dp[i + j] = [dp[i] + (h[i] - h[i + j]).abs, dp[i + j]].min
  end
end
puts dp[n - 1]