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)] P=[0]*(N+1) for fl in range(2): ANS=[float("inf")]*(N+1) Q=[start] ANS[start]=0 while Q: x = heapq.heappop(Q) time = x>>20 fr = x- (time<<20) 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]<<20)+to) P=[P[i]+ANS[i] for i in range(N+1)] NOW=goal while NOW!=start: fr,ind=BACK[NOW] EDGELIST[ind][2]-=1 EDGELIST[ind^1][2]+=1 NOW=fr for i in range(N+1): for j in range(len(EDGE[i])): EDGE[i][j][1]+=P[EDGE[i][j][0]]-P[i] print(ANS[goal])