N, M = map(int, input().split()) dis = [[float('inf')]*N for _ in range(N)] for i in range(N): dis[i][i]=0 for i in range(M): s,t,d = map(int, input().split()) s-=1;t-=1 dis[s][t] = min(dis[s][t],d) for k in range(N): for i in range(N): for j in range(N): dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]) for i in range(N): res = 0 for j in range(N): if dis[i][j]==float('inf'): continue res += dis[i][j] print(res)