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

    a = (0..Y).map {|i| [0] * (i+1) }
    input = (1..n).map { gets.split.map(&:to_i) }.sort_by {|_, j, _| j}
    input.each do |i, j, w|
        b = (0..Y).map { [0] * (Y+1) }
        (0..Y).each do |k|
            (0..k).each do |l|
                max = a[k][l]
                if k >= j
                    x, y = i - 1, l
                    x, y = y, x if y > x
                    m = a[x][y] + w
                    max = m if m > max
                end
                if l >= j
                    m = a[k][i-1] + w
                    max = m if m > max
                end
                b[k][l] = max
            end
        end
        a = b
    end
    p a[-1][-1]
end