# frozen_string_literal: true class Hash def score I.map do |l, r, s| self[l] < self[r] ? s : 0 end.sum end end def solve (0..(N - 1)) .to_a .permutation .map { |p| p.map.with_index { |q, i| { q => i } }.reduce(&:merge).score } .max end N, M = gets.split.map(&:to_i) I = M.times.map { gets.split.map(&:to_i) } puts solve