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

jmp = 1.step(K).to_a
dp = [0]
1.step(N-1) do |i|
  dp[i] = jmp.map {|j| i-j < 0 ? Float::INFINITY : dp[i-j]+(H[i]-H[i-j]).abs }.min
end
puts dp.last
