def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 M = int(input[idx]) idx += 1 INF = 1 << 60 dist = [[INF] * (N + 1) for _ in range(N + 1)] for i in range(1, N + 1): dist[i][i] = 0 for _ in range(M): s = int(input[idx]) idx += 1 t = int(input[idx]) idx += 1 d = int(input[idx]) idx += 1 if d < dist[s][t]: dist[s][t] = d # Floyd-Warshall for k in range(1, N + 1): for i in range(1, N + 1): for j in range(1, N + 1): if dist[i][k] < INF and dist[k][j] < INF: if dist[i][j] > dist[i][k] + dist[k][j]: dist[i][j] = dist[i][k] + dist[k][j] for i in range(1, N + 1): total = 0 for j in range(1, N + 1): if i != j and dist[i][j] < INF: total += dist[i][j] print(total) if __name__ == '__main__': main()