INF = 10 ** 18 n, m = map(int, input().split()) mat = [[INF * 2] * n for _ in range(n)] for _ in range(m): s, t, d = map(int, input().split()) mat[s - 1][t - 1] = d for i in range(n): mat[i][i] = 0 for k in range(n): for i in range(n): for j in range(n): d = mat[i][k] + mat[k][j] if mat[i][j] > d: mat[i][j] = d for row in mat: ans = 0 for x in row: if x < INF: ans += x print(ans)