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

dp = Array.new(n, 0)

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

for i in 2..n - 1
  min_num = Float::INFINITY
  for j in 1..k

    break if i - j < 0

    cost = dp[i-j] + (h[i]-h[i-j]).abs

    if cost < min_num
      min_num = cost
    end

    dp[i] = min_num
  end
end

p dp[n-1]
