#include #define inf (long long)(1e17) using namespace std; using P = pair; int n, m; vector> g, g2; priority_queue, greater

> pq; long long solve(); int main() { cin >> n >> m; g.resize(n); g2.resize(n); for (int i = 0; i < m; ++i) { int x, y, c, d; cin >> x >> y >> c >> d; --x, --y; g[x][y] = g[y][x] = c; g2[x][y] = g2[y][x] = d; } cout << solve() << endl; return 0; } long long solve() { long long res = 0; auto dijk = [](vector> &g, vector &dist, vector &par) { dist.assign(n, inf); par.assign(n, -1); pq.push(P(0, 0)); dist[0] = 0; while (pq.size()) { auto [d, now] = pq.top(); pq.pop(); if (d != dist[now]) continue; for (auto [to, cost] : g[now]) if (d + cost < dist[to]) { dist[to] = d + cost; par[to] = now; pq.push(P(dist[to], to)); } } return dist[n - 1]; }; vector dist, par; res += dijk(g, dist, par); int now = n - 1; while (now != 0) { int to = par[now]; g[to][now] = g[now][to] = -1; now = to; } for (int i = 0; i < n; ++i) for (auto [to, cost] : g[i]) if (cost >= 0) g2[i][to] = cost; return res + dijk(g2, dist, par); }