#include using namespace std; int main(){ int N, M; cin >> N >> M; vector>> E(N); for (int i = 0; i < M; i++){ int u, v, c, d; cin >> u >> v >> c >> d; u--; v--; E[u].push_back(make_tuple(v, c, d)); E[v].push_back(make_tuple(u, c, d)); } vector d1(N, -1); priority_queue, vector>, greater>> pq1; pq1.push(make_pair(0, 0)); while (!pq1.empty()){ long long dd = pq1.top().first; int v = pq1.top().second; pq1.pop(); if (d1[v] == -1){ d1[v] = dd; for (auto edge : E[v]){ int w = get<0>(edge); if (d1[w] == -1){ pq1.push(make_pair(dd + get<1>(edge), w)); } } } } vector pr(N, -1); int t = N - 1; while (t > 0){ for (auto edge : E[t]){ int w = get<0>(edge); if (d1[w] == d1[t] - get<1>(edge)){ pr[t] = w; t = w; break; } } } vector d2(N, -1); priority_queue, vector>, greater>> pq2; pq2.push(make_pair(0, N - 1)); while (!pq2.empty()){ long long dd = pq2.top().first; int v = pq2.top().second; pq2.pop(); if (d2[v] == -1){ d2[v] = dd; for (auto edge : E[v]){ int w = get<0>(edge); if (d2[w] == -1){ if (w == pr[v]){ pq2.push(make_pair(dd + get<2>(edge), w)); } else { pq2.push(make_pair(dd + get<1>(edge), w)); } } } } } cout << d1[N - 1] + d2[0] << endl; }