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