#include using namespace std; using ll = long long; template using vec = vector; template using vvec = vector>; template bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;} template bool chmax(T& a,T b){if(a=0;i--) #define all(x) (x).begin(),(x).end() #define debug(x) cerr << #x << " = " << (x) << endl; typedef pair P; const ll inf = 1e17; class min_cost_flow{ private: int N; struct edge{int to; ll cap,cost; int rev; bool is_rev;}; vector> G; vector h,dist,prevv,preve; public: min_cost_flow(int n){ N = n; G = vector>(N); h = dist = prevv = preve = vector(N,0); } void add_edge(int from,int to,ll cap,ll cost){ G[from].push_back((edge){to,cap,cost,(int) G[to].size(),false}); G[to].push_back((edge){from,0,-cost,(int) G[from].size()-1,true}); } ll answer(int s,int t,ll f){ ll res = 0; fill(h.begin(),h.end(),0); while(f>0){ priority_queue,greater

> Q; fill(dist.begin(),dist.end(),inf); dist[s] = 0; Q.push(P(0,s)); while(!Q.empty()){ P p = Q.top(); Q.pop(); int v = p.second; if(dist[v]0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to] = dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to] = v; preve[e.to] = i; Q.push(P(dist[e.to],e.to)); } } } if(dist[t]==inf) return -1; for(int v=0;v> N >> M; min_cost_flow flow(N); rep(i,M){ int a,b,c,d; cin >> a >> b >> c >> d; a--,b--; flow.add_edge(a,b,1,c); flow.add_edge(a,b,1,d); flow.add_edge(b,a,1,c); flow.add_edge(b,a,1,d); } cout << flow.answer(0,N-1,2) << "\n"; }