from heapq import * n, m = map(int, input().split()) C = [0] * m D = [0] * m edges = [[] for _ in range(n)] for i in range(m): u, v, C[i], D[i] = map(int, input().split()) u -= 1 v -= 1 edges[u].append((v, i)) edges[v].append((u, i)) def dijkstra(): dist = [1 << 60] * n dist[0] = 0 bV = [-1] * n bE = [-1] * n hq = [0] while hq: tmp = heappop(hq) d = tmp // n pos = tmp - n * d if d > dist[pos]: continue for npos, i in edges[pos]: nd = d + C[i] if nd < dist[npos]: dist[npos] = nd bV[npos] = pos bE[npos] = i heappush(hq, nd * n + npos) p = n - 1 while p != 0: i = bE[p] C[i] = D[i] p = bV[p] return dist[-1] ans = dijkstra() + dijkstra() print(ans)