import sys from heapq import * input = sys.stdin.readline INF = 1001001001 mask32 = (1 << 32) - 1 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 が持つのは、(コスト << 32) + 頂点 # dijkstra 1回目 pq = [0] dp = [INF] * N dp[0] = 0 parent = [-1] * N while pq: t = heappop(pq) cost = t >> 32 v = t & mask32 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] << 32) + nv) first_cost = dp[N - 1] v = N - 1 while v != 0: p = parent[v] adj[v][p] = adj[p][v] = uv2d[(v, p)] v = p # pq が持つのは、(コスト << 32) + 頂点 # dijkstra 2回目 pq = [N - 1] dp = [INF] * N dp[N - 1] = 0 # parent = [-1] * N while pq: t = heappop(pq) cost = t >> 32 v = t & mask32 if cost > dp[v]: continue for nv, dist in enumerate(adj[v]): if dp[nv] > dp[v] + dist: dp[nv] = dp[v] + dist # parent[nv] = v heappush(pq, (dp[nv] << 32) + nv) second_cost = dp[0] print(first_cost + second_cost)