#include using namespace std; #define ll long long #define rep(i,n) for(int i=0;i=0;i--) #define rrep2(i,n,k) for(int i=n-1;i>=n-k;i--) #define vll(n,i) vector(n,i) #define v2ll(n,m,i) vector>(n,vll(m,i)) #define v3ll(n,m,k,i) vector>>(n,v2ll(m,k,i)) #define v4ll(n,m,k,l,i) vector>>>(n,v3ll(m,k,l,i)) #define all(v) v.begin(),v.end() #define chmin(k,m) k = min(k,m) #define chmax(k,m) k = max(k,m) #define Pr pair #define Tp tuple #define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr) using Graph = vector>; const ll mod = 998244353; template struct modint{ uint64_t val; constexpr modint(const int64_t val_=0) noexcept:val((val_%int64_t(mod)+int64_t(mod))%int64_t(mod)){} constexpr modint operator-() const noexcept{ return modint(*this)=mod-val; } constexpr modint operator+(const modint rhs) const noexcept{ return modint(*this)+=rhs; } constexpr modint operator-(const modint rhs) const noexcept{ return modint(*this)-=rhs; } constexpr modint operator*(const modint rhs) const noexcept{ return modint(*this)*=rhs; } constexpr modint operator/(const modint rhs) const noexcept{ return modint(*this)/=rhs; } constexpr modint &operator+=(const modint rhs) noexcept{ val+=rhs.val; val-=((val>=mod)?mod:0); return (*this); } constexpr modint &operator-=(const modint rhs) noexcept{ val+=((val>=1; } return (*this)*=now; } modint & operator++(){ val++; if (val == mod) val = 0; return *this; } modint operator++(int){ modint res = *this; ++*this; return res; } constexpr bool operator==(const modint rhs) noexcept{ return val==rhs.val; } constexpr bool operator!=(const modint rhs) noexcept{ return val!=rhs.val; } friend constexpr ostream &operator<<(ostream& os,const modint x) noexcept{ return os<<(x.val); } friend constexpr istream &operator>>(istream& is,modint& x) noexcept{ uint64_t t; is>>t,x=t; return is; } }; typedef modint mint; mint pw(long long a,long long b,long long m = mod){ if(a%m==0) return mint(0); if(b==0) return mint(1); else if(b%2==0){ long long x = pw(a,b/2,m).val; return mint(x*x); } else{ long long x = pw(a,b-1,m).val; return mint(a*x); } } mint modinv(long long a, long long m = mod) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; return mint(u); } #define vm(n,i) vector(n,i) #define v2m(n,m,i) vector>(n,vm(m,i)) #define v3m(n,m,k,i) vector>>(n,v2m(m,k,i)) #define v4m(n,m,k,l,i) vector>>>(n,v3m(m,k,l,i)) //Graph struct graph { long long N; vector>> G; vector par_v; vector par_e; int edge_count = 0; graph(long long n) { N = n; G = vector>>(N); par_v = vector(N,-1); par_e = vector(N,-1); } void unite(long long a,long long b,long long cost = 1,bool directed = false){ G[a].emplace_back(b,cost,edge_count); if(!directed) G[b].emplace_back(a,cost,edge_count); edge_count++; } }; vector dijkstra(graph &g,int s){ ll N = g.G.size(); priority_queue, greater> go; ll x,y,a,t,l; ll inf = 2e18; vector dist(N,inf); go.push(make_pair(0,s)); dist[s] = 0; Pr p; while(!go.empty()){ p = go.top(); go.pop(); x = p.first; y = p.second; if(x>dist[y]) continue; for(auto q:g.G[y]){ //G[y]:Graph a = get<0>(q); t = get<1>(q); l = get<2>(q); if(x+t shortest_e(graph &g,int goal,int start = 1){ int now = goal; vector path; while(now!=-1){ path.push_back(g.par_e[now]); if(now==start) break; now = g.par_v[now]; } path.pop_back(); reverse(path.begin(),path.end()); return path; } int main() { riano_; ll ans = 0; ll N,M; cin >> N >> M; vector> ed(2); graph G(N+1); rep(i,M){ ll a,b,c,d; cin >> a >> b >> c >> d; ed[0].emplace_back(a,b,c); ed[1].emplace_back(a,b,d); G.unite(a,b,c); } auto d1 = dijkstra(G,1); auto p = shortest_e(G,N); //for(ll id:p) cout << id << endl; vector used(M,false); for(ll id:p) used[id] = true; graph G2(N+1); rep(i,M){ if(used[i]){ auto[a,b,d] = ed[1][i]; G2.unite(a,b,d); } else{ auto[a,b,c] = ed[0][i]; G2.unite(a,b,c); } } auto d2 = dijkstra(G2,1); ans = d1[N]+d2[N]; cout << ans << endl; }