$N,$K = gets.split.map(&:to_i)
$H = gets.split.map(&:to_i)
$INF = 1 << 60

class Node
  attr_accessor :min_cost, :hight

  def initialize(h)
    @hight = h
    @min_cost = $INF
  end
end

dp = Array.new($N){|i| Node.new($H[i])}

dp[0].min_cost = 0

dp.each_with_index do |v, i|
  $K.times do |k|
    if i-k-1 < 0
      break
    else
    #i-(k-1)番目からiに飛んでくるコストを計算する。
      cost = (dp[i].hight - dp[i-k-1].hight).abs + dp[i-k-1].min_cost
      dp[i].min_cost = cost if cost < dp[i].min_cost
    end
  end
end

puts dp[$N-1].min_cost
