# frozen_string_literal: true class Array def score I.map { |l, r, s| index(l) < index(r) ? s : 0 }.sum end end def solve (0..(N - 1)).to_a.permutation.map(&:score).max end N, M = gets.split.map(&:to_i) I = M.times.map { gets.split.map(&:to_i) } puts solve