from sys import stdin N,M = map(int,stdin.readline().split()) lis = [[float("inf")] * N for i in range(N)] for i in range(M): s,t,d = map(int,stdin.readline().split()) s -= 1 t -= 1 lis[s][t] = d for i in range(N): lis[i][i] = 0 for k in range(N): for i in range(N): for j in range(N): lis[i][j] = min(lis[i][j] , lis[i][k] + lis[k][j]) ans = 0 for i in range(N): ans = 0 for j in range(N): if lis[i][j] != float("inf"): ans += lis[i][j] print (ans)