first_row = gets.split.map(&:to_i) n = first_row[0] m = first_row[1] input = STDIN.read.split("\n") Criteria = Struct.new(:item1, :item2, :score) crt = [] input.each do |item| tmp = item.split.map!(&:to_i) str = Criteria.new(tmp[0], tmp[1], tmp[2]) crt << str end ans = 0 (0..(n - 1)).to_a.permutation do |i| tmp = 0 crt.each do |crt| if i.index(crt.item1) < i.index(crt.item2) tmp += crt.score end ans = [ans, tmp].max end end puts ans