INF = 1 << 60

n, k = gets.split.map(&:to_i)
hs = gets.split.map(&:to_i)

dp = [0]
# refer ahead
last = n - 1
0.upto(last) do |i|
  (i + 1).upto([i + k, last].min) do |j|
    # inline change min
    cost = dp[i] + (hs[i] - hs[j]).abs
    dp[j] = cost if dp.fetch(j, INF) > cost
  end
end

p dp.last
