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