import java.util.*; import java.io.*; import java.math.*; public class No90 { // public static int[] ary; public static String[] ary; public static int MAX = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int y = 3 * m; // int[] tmp = new int[y]; ary = new String[y]; for(int i = 0; i < y; i++) { // tmp[i] = sc.nextInt(); // ary[i] = sc.nextInt(); ary[i] = sc.next(); } // 商品の文字列作成 String str = ""; for(int i = 0; i < n; i++) { str = str + i; } // System.out.println("str = " + str); permutation(str, ""); System.out.println(MAX); // for(int i = 0; i < y; i=i+3) { // System.out.print(ary[i] + " " + ary[i+1] + " " + ary[i+2]); // System.out.println(""); // } } public static void permutation(String q, String ans){ // Base Case if(q.length() <= 1) { // System.out.println(ans + q); String str = ans + q; // System.out.println("str = " + str); int total = 0; for(int i = 0; i < ary.length; i=i+3) { String it1 = ary[i]; String it2 = ary[i+1]; int sc = Integer.parseInt(ary[i+2]); // System.out.println(ary[i] + "のindex = " + str.indexOf(it1)); // System.out.println(ary[i+1] + "のindex = " + str.indexOf(it2)); if(str.indexOf(it1) < str.indexOf(it2)) { total += sc; } } // System.out.println("total = " + total); MAX = MAX < total? total : MAX; } // General Case else { for (int i = 0; i < q.length(); i++) { permutation(q.substring(0, i) + q.substring(i + 1), ans + q.charAt(i)); } } } }