from heapq import heappush,heappop,heapify INF=float('inf') def dijkstra(G,s,n): que=[(0,s)] dist=[INF]*n last=[-1 for i in range(n)] dist[s]=0 while que: mincost,u=heappop(que) if(mincost>dist[u]): continue for v,c in G[u].items(): if(dist[u]+cb: a,b=b,a G[v][last[v]]=nextc[(a,b)] G[last[v]][v]=nextc[(a,b)] update(last[v]) N,M=map(int,input().split()) G=[{} for _ in range(N)] nextc={} for _ in range(M): u,v,c,d=map(int,input().split()) u-=1 v-=1 if u>v: v,u=u,v G[u][v]=c G[v][u]=c nextc[(u,v)]=d dist,last=dijkstra(G,0,N) ans=dist[-1] update(N-1) dist,last=dijkstra(G,N-1,N) ans+=dist[0] print(ans)