N, K = gets.split.map(&:to_i)
hs = gets.split.map(&:to_i)

dp = Array.new(N, 10 ** 5)
dp[0] = 0
dp[1] = (hs[0] - hs[1]).abs

for i in 2..(N-1) do 
  for k in 1..([K, i].min) do
    dp[i] = [dp[i-k] + (hs[i-k] - hs[i]).abs, dp[i]].min
  end
end

puts dp[N-1]