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

INF = 10**4

dp = [INF]*n

dp[0] = 0
dp[1] = (h[1]-h[0]).abs

1.upto(n-1) do |i|
  1.upto(k) do |j|
    break if i - j < 0
    dp[i] = [dp[i-j] + (h[i]-h[i-j]).abs, dp[i]].min
  end
end

p dp[n-1]

