#include using namespace std; template using vp = vector>; template using pque = priority_queue; template using lpque = priority_queue,greater>; using ll = long long; using pint = pair; using pll = pair; using pil = pair; using pli = pair; using vint = vector; using vll = vector; using qint = queue; using pqint = pque; using qll = queue; using pqll = pque; constexpr double PI = 3.141592653589793; constexpr int INTINF = (1<<30)-1; constexpr ll LLINF = (1LL<<62)-1; constexpr int MPRIME = 1000000007; constexpr int MPRIME9 = 998244353; constexpr ll MMPRIME = (1LL<<61)-1; constexpr char newl = '\n'; #define len length() #define pushb push_back #define fi first #define se second #define all(name) name.begin(),name.end() #define rall(name) name.rbegin(),name.rend() #define gsort(vbeg,vend) sort(vbeg,vend,greater<>()) template struct matrix{ private: vector> mat; public: matrix() : matrix(0,0) {} matrix(int h, int w) { resize(h,w); } matrix(int h, int w, T init) { resize(h,w,init); } void resize(int h, int w) { mat=vector>(h,vector(w)); } void resize(int h, int w, T init) { mat=vector>(h,vector(w,init)); }; void in() { for(int i=0; i>mat[i][j]; } } void out() { for(int i=0; i &operator[](int idx) { assert(0<=idx && idx inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline void init(T& v) { for(auto &a: v) cin>>a; } template inline void init(vector>& v) { for(auto &a: v) cin>>a.first>>a.second; } template inline void init(T& v, N n) { v.resize(n); for(auto &a: v) cin>>a; } template inline void init(vector>& v, N n) { v.resize(n); for(auto &a: v) cin>>a.first>>a.second; } template inline void out(T a) { cout< inline void out(T a, U... alist) { cout<(alist)...); } template void resiz(N n) { //empty } template void resiz(N n, T&& hd, U&&... tl) { hd.resize(n); resiz(n,forward(tl)...); } long long binpow(long long a, long long ex, long long p=MMPRIME) { long long res = 1; while(ex > 0) { if(ex & 1) (res*=a) %= p; ex>>=1; (a*=a) %= p; } return res; } struct mincostflow { private: struct edge { int next; int rev; long long cap; long long cost; edge(int next, int rev, long long cap, long long cost) : next(next), rev(rev), cap(cap), cost(cost) {} }; public: const long long inf = (1LL<<62)-1; private: const int vnum; vector> G; vector pot; vector pv, pe; public: mincostflow(int N) : vnum(N), G(N), pot(N), pv(N), pe(N) {} void add(int from, int to, long long cap, long long cost) { G[from].push_back(edge(to, G[to].size(), cap, cost)); G[to].push_back(edge(from, G[from].size()-1, 0, -cost)); } private: long long bellman_ford(int s, int t, int &f) { pot.assign(vnum, inf); pv.assign(vnum, -1); pe.assign(vnum, -1); pot[s] = 0; for(int i=0; i0 && pot[ed.next]>pot[j]+ed.cost) { if(i == vnum-1) return -inf; pot[ed.next] = pot[j]+ed.cost; pv[ed.next] = j; pe[ed.next] = k; } } } } int add_f = f; for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap); f -= add_f; for(int v=t; v!=s; v=pv[v]) { edge &ed = G[pv[v]][pe[v]]; ed.cap -= add_f; G[v][ed.rev].cap += add_f; } return pot[t]*add_f; } long long dijkstra(int s, int t) { long long ans = 0; priority_queue, vector>, greater>> q; vector dist(vnum, inf); pv.assign(vnum, -1); pe.assign(vnum, -1); q.push(make_pair(0LL,s)); dist[s] = 0; while(!q.empty()) { long long d = q.top().first, v = q.top().second; q.pop(); if(dist[v] < d) continue; for(int i=0; i0 && dist[ed.next]>nd) { dist[ed.next] = nd; pv[ed.next] = v; pe[ed.next] = i; q.push(make_pair(nd,ed.next)); } } } if(dist[t] == inf) return inf; ans = dist[t]+pot[t]; for(int v=0; v 0) { long long restmp = dijkstra(s, t); int add_f = f; if(restmp == inf) return inf; for(int v=t; v!=s; v=pv[v]) add_f = min((long long)add_f, G[pv[v]][pe[v]].cap); f -= add_f; res += restmp*add_f; for(int v=t; v!=s; v=pv[v]) { edge &ed = G[pv[v]][pe[v]]; ed.cap -= add_f; G[v][ed.rev].cap += add_f; } } return res; } }; int N,M; void input() { cin>>N>>M; } void solve() { mincostflow mcf(N+1); for(int i=0; i>u>>v>>c>>d; mcf.add(u,v,1,c); mcf.add(u,v,1,d); mcf.add(v,u,1,c); mcf.add(v,u,1,d); } cout<