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