n, k = gets.chomp.split.map(&:to_i)
hs = gets.chomp.split.map(&:to_i)
dp = [0]

1.upto(n - 1) do |i|
  min = Float::INFINITY
  [0, i - k].max.upto(i - 1) do |j|
    cost = dp[j] + (hs[i] - hs[j]).abs
    min = cost if cost < min
  end
  dp[i] = min
end

puts dp[n - 1]