N, K = gets.split.map(&:to_i)
H = gets.split.map(&:to_i)
dp = Array.new(N, Float::INFINITY)
dp[0] = 0
(1 ... N).each do |i|
  dp[i] = ([i - K, 0].max ... i).map{|j| dp[j] + (H[j] - H[i]).abs }.min
end
puts dp[-1]
