n, k = gets.split.map(&:to_i)
hs = gets.split.map(&:to_i)
dp = [0, 0, (hs[1] - hs[0]).abs]
(3..n).each do |i|
  dp[i] = 1000000000000
  (1..[k, i].min).each do |j|
    dp[i] = [dp[i], dp[i - j] + (hs[i - 1] - hs[i - 1 - j]).abs].min
  end
end
puts dp[n]