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

INF = 100_100

@dp = Array.new(100_100).map{ INF }

@h = gets.chomp.split.map(&:to_i)

@dp[0] = 0
@n.times do |i|
  (1..@k).each do |j|
    v = @dp[i] + (@h[i] - (@h[i+j] || 0)).abs
    @dp[i+j] = v if @dp[i+j] > v
  end
end

puts @dp[@n-1]
