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

dp = Array.new(N, 10**10)

dp[0] = 0

def min(a,b)
  a < b ? a : b
end

1.upto(N-1) do |i|
  k = min(K, i)

  1.upto(k) do |j|
    tmp = (h[i-j] - h[i]).abs + dp[i-j]
    dp[i] = min(dp[i], tmp)
  end
end

p dp.last
