n, m = map(int, input().split()) inf = 10**18 dist = [[inf]*n for _ in range(n)] for i in range(n): dist[i][i] = 0 for _ in range(m): s, t, d = map(int, input().split()) s -= 1 t -= 1 dist[s][t] = min(dist[s][t],d) for i in range(n): for j in range(n): for k in range(n): dist[j][k] = min(dist[j][k], dist[j][i]+dist[i][k]) for i in range(n): ans = 0 for j in range(n): if dist[i][j] != inf: ans += dist[i][j] print(ans)