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|
  min = Float::INFINITY

  1.upto(K) do |j|
    idx = i - j

    break if idx < 0

    m = dp[idx] + (hs[i] - hs[idx]).abs

    min = m if m < min

    break if m == dp[idx]
  end

  dp[i] = min
end

puts dp.last
