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

dp = []
dp[0] = 0

1.upto(n-1) do | i |
  dp[i] = dp[i-1] + (hs[i] - hs[i-1]).abs # 1jumpで来たケースをとりあえず代入しとく.

  [i-k, 0].max.upto(i-2) do | j |
    cost = dp[j] + (hs[i]-hs[j]).abs
    dp[i] = cost if dp[i] > cost
  end
end
p dp[-1]