n,m = map(int,input().split()) d = [[0]*n for _ in range(n)] for _ in range(m): x,y,c = map(int,input().split()) d[x][y] += c from random import shuffle r = list(range(n)) v = 0 for _ in range(1000): shuffle(r) while True: for i in range(n-1): if d[r[i]][r[i+1]] < d[r[i+1]][r[i]]: r[i],r[i+1] = r[i+1],r[i] break else: break s = 0 for i in range(n): for j in range(i+1,n): s += d[r[i]][r[j]] if v < s: v = s ans = r[:] print(v) print(*ans)