# frozen_string_literal: true

n, k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)
dp = Array.new(n, 100_000_000_000)

dp[0] = 0
(n - 1).times do |i|
  count = [k, n - i - 1].min
  1.upto(count) do |x|
    tmp = dp[i] + (h[i] - h[i + x]).abs
    dp[i + x] = tmp if dp[i + x] > tmp
  end
end

puts dp[n - 1]
