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

dp = Array.new(n+10, 10**20)
dp[0] = 0
dp[1] = (hs[0] - hs[1]).abs
(2..n-1).each do |i|
  (1..[k,i].min).each do |k|
    dp[i] = [dp[i-k] + (hs[i-k] - hs[i]).abs, dp[i]].min
  end
end
puts dp[n-1]
