#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; using size_type = ssize_t; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template struct multi_dim_array { using type = std::array::type, Head>; }; template struct multi_dim_array { using type = std::array; }; template using mdarray = typename multi_dim_array::type; template void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable::value) { t = f(args...); } else { for (size_type i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } template vec make_v(size_type sz) { return vec(sz); } template auto make_v(size_type hs, Tail&&... ts) { auto v = std::move(make_v(std::forward(ts)...)); return vec(hs, v); } namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } template T ceil_pow2(T bound) { T ret = 1; while (ret < bound) ret *= 2; return ret; } template T ceil_div(T a, T b) { return a / b + !!(a % b); } int main() { ll n, m; std::cin >> n >> m; using raw_edge = std::tuple; vec edges(m); for (auto &[ u, v, c, d ] : edges) { std::cin >> u >> v >> c >> d; u--; v--; } vec cost1(m), cost2(m); vvec g(n); for (ll i = 0; i < m; i++) { auto [ u, v, c , d ] = edges[i]; cost1[i] = c; cost2[i] = d; g[u].emplace_back(v, i); g[v].emplace_back(u, i); } constexpr ll inf = 5e15; vec used(m, false); vec prev(m, -inf); auto get_adj = [&](ll u, pll e) { auto [ a, b ] = e; if (u == a) return b; else return a; }; auto get_cost = [&](ll idx) { if (used[idx]) return cost2[idx]; return cost1[idx]; }; auto dijk = [&] { std::priority_queue, std::greater> pq; vec dists(n, inf); dists[0] = 0; pq.emplace(0, 0, -1); while (pq.size()) { auto [ d, cur, eidx ] = pq.top(); pq.pop(); if (dists[cur] < d) continue; prev[cur] = eidx; for (auto [ nxt, i ] : g[cur]) { auto nd = d + get_cost(i); if (dists[nxt] <= nd) continue; dists[nxt] = nd; pq.emplace(nd, nxt, i); } } return dists; }; auto d1 = dijk(); ll cur = n - 1; while (cur) { auto idx = prev[cur]; used[idx] = true; auto [ u, v, c, d ] = edges[idx]; auto p = get_adj(cur, pll(u, v)); cur = p; } auto d2 = dijk(); std::cout << d1.back() + d2.back() << "\n"; return 0; }