import java.util.Random; import java.util.Scanner; public class Main { private static double sigmoid(double x) { return 1 / (1 + Math.exp(-x)); } public static void main(String[] argv) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[][] p = new int[n][n]; for (int i = 0; i < m; i++) { int mae = scanner.nextInt(); int ato = scanner.nextInt(); p[ato][mae] = scanner.nextInt(); } int[] ans = new int[n]; for (int i = 0; i < n; i++) ans[i] = i; Random r = new Random(); int count = 0; while (count++ < 10000) { int a = r.nextInt(n); int b = r.nextInt(n); if (a == b) continue; if (a > b) { int t = b; b = a; a = t; int diff = p[ans[a]][ans[b]] - p[ans[b]][ans[a]]; for (int i = a + 1; i <= b - 1; i++) { diff -= p[ans[b]][ans[i]]; diff += p[ans[i]][ans[a]]; } double prob = sigmoid(diff); if (r.nextDouble() < prob) { int tt = ans[b]; ans[b] = ans[a]; ans[a] = tt; } } } int score = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { score += p[ans[j]][ans[i]]; } } System.out.println(score); for (int i = 0; i < n; i++) { System.out.print(ans[i] + " "); } } }