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