import sys input = sys.stdin.readline import heapq N,M=map(int,input().split()) EDGE=[[] for i in range(N+1)] EDGELIST=[] eind=0 for i in range(M): u,v,c,d=map(int,input().split()) X=[v,c,1,eind] EDGE[u].append(X) EDGELIST.append(X) eind+=1 X=[u,-c,0,eind] EDGE[v].append(X) EDGELIST.append(X) eind+=1 X=[u,c,1,eind] EDGE[v].append(X) EDGELIST.append(X) eind+=1 X=[v,-c,0,eind] EDGE[u].append(X) EDGELIST.append(X) eind+=1 X=[v,d,1,eind] EDGE[u].append(X) EDGELIST.append(X) eind+=1 X=[u,-d,0,eind] EDGE[v].append(X) EDGELIST.append(X) eind+=1 X=[u,d,1,eind] EDGE[v].append(X) EDGELIST.append(X) eind+=1 X=[v,-d,0,eind] EDGE[u].append(X) EDGELIST.append(X) eind+=1 start=1 goal=N BACK=[[-1,-1] for i in range(N+1)] LA=0 for fl in range(2): ANS=[1<<60]*(N+1) Q=[(0,start)] ANS[start]=0 while Q: time,fr = heapq.heappop(Q) if time > ANS[fr]: continue for to,cost,vol,ind in EDGE[fr]: if vol>0 and ANS[to]>ANS[fr]+cost: ANS[to]=ANS[fr]+cost BACK[to]=[fr,ind] heapq.heappush(Q,(ANS[to],to)) LA+=ANS[goal] NOW=goal while NOW!=start: fr,ind=BACK[NOW] EDGELIST[ind][2]-=1 EDGELIST[ind^1][2]+=1 NOW=fr print(LA)