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

dp = [0]
1.step(N-1) do |i|
  min = Float::INFINITY
  1.step(K) do |j|
    index = i - j
    break if index < 0
    cost = dp[index] + (H[i]-H[index]).abs
    min = cost if cost < min
  end
  dp[i] = min
end
puts dp.last
