n=gets l=$<.map{|s|s.split.map &:to_i}.sort_by{|e|-e[2]} m=t=0;l.map{|e|t+=e[2]} d=[*0...n.to_i] d.permutation.each{|a|s=t;l.take_while{|e|x,y,v=*e;a.index(y)m};m