N,K = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
dp=Array.new(N,0)
for i in 1...N
    cnt=Float::INFINITY
    if 1<=i && i<K
        for j in 0..i-1
            com=dp[j]+(as[j]-as[i]).abs
            cnt=cnt > com ? com : cnt
        end
        dp[i]=cnt
    else
        for j in i-K..i-1
            com=dp[j]+(as[j]-as[i]).abs
            cnt=cnt > com ? com : cnt
        end
        dp[i]=cnt
    end
end
puts dp.last
