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

dp = Array.new(n, Float::INFINITY)
dp[0] = 0

n.times do |i|
    
    k.times do |j|
        if i+j+1 >= n
            next
        end
        dp[i+j+1] = [dp[i]+(ary[i+j+1]-ary[i]).abs, dp[i+j+1]].min
    end
end

puts dp[n-1]

