import sys input = sys.stdin.readline import heapq N,M=map(int,input().split()) EDGE=[dict() for i in range(2*N+1)] for i in range(M): u,v,c,d=map(int,input().split()) EDGE[u][v]=[c,1] EDGE[u][N+v]=[d,1] EDGE[u][N+u]=[0,float("inf")] EDGE[N+u][u]=[0,float("inf")] EDGE[N+v][u]=[d,0] EDGE[v][u]=[c,1] EDGE[v][N+u]=[d,1] EDGE[v][N+v]=[0,float("inf")] EDGE[N+v][v]=[0,float("inf")] EDGE[N+u][v]=[d,0] start=1 goal=N BACK=[-1]*(2*N+1) LA=0 for flow in range(2): ANS=[float("inf")]*(2*N+1) Q=[(0,start)] ANS[start]=0 while Q: time,fr = heapq.heappop(Q) if time > ANS[fr]: continue for to in EDGE[fr]: cost,flow=EDGE[fr][to] if flow>0 and ANS[to]>ANS[fr]+cost: ANS[to]=ANS[fr]+cost BACK[to]=fr heapq.heappush(Q,(ANS[to],to)) LA+=ANS[goal] NOW=goal while NOW!=start: fr=BACK[NOW] EDGE[fr][NOW][1]-=1 EDGE[NOW][fr][1]+=1 NOW=fr print(LA)