N, K = gets.split.map(&:to_i)
hs = gets.split.map(&:to_i)

dp = Array.new(N)
dp[0] = 0

(1...N).each do |i|
  dp[i] = Array.new(K).map.with_index(1) {|_, j|
    if i - j < 0
      nil
    else
      dp[i - j] + (hs[i] - hs[i - j]).abs
    end
  }.compact.min
end

puts dp.last

