from collections import deque n,m=map(int,input().split()) num=[[[] for _ in range(n)] for z in range(n)] for i in range(m): s,t,d=map(int,input().split()) num[s-1][t-1].append(d) num2=[[] for _ in range(n)] for i in range(n): for j in range(n): if len(num[i][j])!=0: num2[i].append([j,min(num[i][j])]) for i in range(n): queue=deque() ans=[10**18]*n ans[i]=0 queue.append(i) while len(queue)!=0: num1=queue.popleft() num3=ans[num1] num4=num2[num1] for j in range(len(num4)): if ans[num4[j][0]]==10**18: ans[num4[j][0]]=num3+num4[j][1] queue.append(num4[j][0]) if ans[num4[j][0]]>num3+num4[j][1]: ans[num4[j][0]]=num3+num4[j][1] queue.append(num4[j][0]) ans2=0 for j in range(n): if ans[j]!=10**18: ans2+=ans[j] print(ans2)