from sys import stdin N,M = map(int,stdin.readline().split()) e = [] for i in range(M): s,t,d = map(int,stdin.readline().split()) s -= 1 t -= 1 e.append((s,t,d)) for v in range(N): d = [float("inf")] * N d[v] = 0 for i in range(N): for st,go,dis in e: d[go] = min(d[go] , d[st] + dis) ans = 0 for i in d: if i != float("inf"): ans += i #print (d) print (ans)