N, K = gets.split.map(&:to_i)
H = gets.split.map(&:to_i)
DP = Array.new(N, 10000)
DP[0] = 0
(1...N).each {|i|
  DP[i] = ([0, i - K].max...i).map{|j|
    # puts "#{i}(#{H[i]}) #{j}(#{H[j]}) -> #{(H[i] - H[j]).abs}"
    DP[j] + (H[i] - H[j]).abs
  }.min
  # p "#{i} -> #{DP[i]}"

}
p DP[N - 1]