N,K = gets.split.map &:to_i
H = gets.split.map &:to_i
DP = Array.new(N, 10**4)
DP[0] = 0

(N-1).times do |i|
  k = K < N - i - 1 ? K : N - i - 1
  k.times do |j|
    n = i + j + 1
    cost = (H[n] - H[i]).abs + DP[i]
    DP[n] = cost if cost < DP[n]
  end
end

p DP[N-1]