loop do
    n = gets.to_i
    break if n == 0

    applications = (1..n).map do
        gets.split.map(&:to_i)
    end 
    applications.sort_by!{|i, j, w| [j, i]} 

    hash = {[0, 0] => 0}
    applications.each do |i, j, w|
        next_hash = Hash.new(0)
        hash.each do |(a, b), profit|
            next_hash[[a, b]] = profit if profit > next_hash[[a, b]] 
            if a < i 
                t = [j, b].sort
                next_hash[t] = profit + w if profit + w > next_hash[t]
            end
            if b < i 
                t = [a, j].sort
                next_hash[t] = profit + w if profit + w > next_hash[t]
            end
        end
        hash = next_hash
    end 
    p hash.values.max
end