N, M = map(int, input().split()) inf = 10 ** 18 G = [[inf] * N for i in range(N)] for i in range(N): G[i][i] = 0 for i in range(M): s, t, d = map(int, input().split()) s, t = s - 1, t - 1 G[s][t] = min(G[s][t], d) for k in range(N): for i in range(N): for j in range(N): G[i][j] = min(G[i][j], G[i][k] + G[k][j]) for i in range(N): ans = 0 for j in range(N): if G[i][j] == inf: continue ans += G[i][j] print(ans)