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

hs_distances = []
(1..n).each do |i|
  hs_distances[i] = []
  (1..n).each do |j|
    hs_distances[i][j] = (hs[i - 1] - hs[j - 1]).abs
  end
end

dp = [0, 0, (hs[1] - hs[0]).abs]
(3..n).each do |i|
  dp[i] = Float::INFINITY
  (1..[k, i - 1].min).each do |j|
    dp[i] = [dp[i], dp[i - j] + hs_distances[i][i - j]].min
  end
end
puts dp[n]