from collections import defaultdict def culc_inc(n, m): if m in D[n]: return D[n][m] else: return 0 def insertion(): sum = 0 L = [NL.pop()] while NL: inc = 0 inc_L = 0 inc_R = 0 max_inc = 0 max_index = 0 n = NL.pop() for i in reversed(range(len(L))): inc_R = culc_inc(L[i], n) + inc_L inc_L = culc_inc(n, L[i]) + inc_L inc = max(inc_L, inc_R) if inc > max_inc: max_inc = inc max_index = (i+1 if inc_L < inc_R else i) else: max_inc += culc_inc(L[i], n) L.insert(max_index, n) sum += max_inc return sum N, M = map(int,input().split()) NL = [i for i in range(N)] D = defaultdict(dict) for i in range(M): item1, item2, score = map(int,input().split()) D2 = {item2: score} D[item1].update(D2) print(insertion())