N, K = gets.chomp.split(" ").map(&:to_i)
H = gets.chomp.split(" ").map(&:to_i)

dp = {}
dp[0] = 0

1.upto(N-1) do |i|
  1.upto(K) do |k|
    next if i - k < 0
    dp[i] = [dp[i], dp[i-k] + (H[i-k] - H[i]).abs].compact.min
  end
end

puts dp[N-1]
