import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); String [][]item = new String[M][2]; int []score = new int[M]; for(int i = 0; i < M; i++) { for(int j = 0; j < 2; j++) { item[i][j] = scan.next(); } score[i] = scan.nextInt(); } scan.close(); int l = 1; String s = ""; for(int i = 0; i < N; i++) { l = l *(i + 1); s += Integer.toString(i); } s += " "; //String s = "012345678"; int max = 0; for(int i = 1; i <= l; i++) { int p = 0; //System.out.println(s); for(int j = 0; j < M; j++) { int k1 = s.indexOf(item[j][0]); int k2 = s.indexOf(item[j][1]); if(k1 < k2) { p += score[j]; } } if(max < p) { max = p; } int a = i; int b = 2; while(a % b == 0) { a /= b++; } s = new StringBuffer(s.substring(0, b)).reverse() + s.substring(b); } System.out.println(max); } }