def BellmanFord(N, G, s): INF = 1<<64 dist = [INF for _ in range(N)] dist[s] = 0 for cnt in range(N): Update = False for pos,nxt,cost in G: if dist[pos] + cost < dist[nxt]: dist[nxt] = dist[pos] + cost Update = True if not Update: break if cnt == N - 1: return -1 return dist INF = 1<<64 N,M = map(int,input().split()) G = [] for _ in range(M): s,t,d = map(int,input().split()) s -= 1 t -= 1 G.append((s, t, d)) for i in range(N): dist = BellmanFord(N, G, i) ans = 0 for d in dist: if d != INF: ans += d print(ans)