import sys from heapq import * input = sys.stdin.readline INF = float("inf") N, M = map(int, input().split()) adj = [dict() for _ in range(N)] uv2d = dict() for _ in range(M): u, v, c, d = map(int, input().split()) u -= 1 v -= 1 adj[u][v] = adj[v][u] = c uv2d[(u, v)] = uv2d[(v, u)] = d # pq が持つのは、(コスト, 頂点) # dijkstra 1回目 pq = [(0, 0)] dp = [INF] * N dp[0] = 0 parent = [-1] * N while pq: cost, v = heappop(pq) if cost > dp[v]: continue for nv, dist in adj[v].items(): if dp[nv] > dp[v] + dist: dp[nv] = dp[v] + dist parent[nv] = v heappush(pq, (dp[nv], nv)) # print(dp) v = N - 1 while v != 0: p = parent[v] adj[v][p] = adj[p][v] = uv2d[(v, p)] v = p # pq が持つのは、(コスト, 頂点) # dijkstra 2回目 pq = [(dp[N - 1], N - 1)] for i in range(N - 1): dp[i] = INF # parent = [-1] * N while pq: cost, v = heappop(pq) if cost > dp[v]: continue for nv, dist in adj[v].items(): if dp[nv] > dp[v] + dist: dp[nv] = dp[v] + dist # parent[nv] = v heappush(pq, (dp[nv], nv)) # print(dp) print(dp[0])