N, K = gets.split.map(&:to_i)
H = gets.split.map(&:to_i)
dp = Array.new(N, Float::INFINITY)

N.times do |i|
  if i == 0
    dp[i] = 0
  elsif i < K
    (1..i).each do |j|
      dp[i] = [dp[i], dp[i - j] + (H[i] - H[i - j]).abs].min
    end
  else
    (1..K).each do |j|
      dp[i] = [dp[i], dp[i - j] + (H[i] - H[i - j]).abs].min
    end
  end
end

puts dp[N - 1]