# frozen_string_literal: true

def chmin(a, b)
  a > b ? b : a
  end

n, k = gets.split.map(&:to_i)
h = gets.split.map(&:to_i)
dp = Array.new(100_010, 1_000_000_007)
dp[0] = 0

(1..n - 1).each do |i|
  if i >= k
    (1..k).each do |j|
      dp[i] = chmin(dp[i], dp[i - j] + (h[i - j] - h[i]).abs)
    end
  end
  dp[i] = chmin(dp[i - 1] + (h[i] - h[i - 1]).abs, dp[i])
end
puts dp[n - 1]
