#include #include #include #include #include #include #include #include using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; class mcf{ struct edge{ long long to, cap, cost, rev; }; long long V; vector> G; vector h; vector dist; vector prevv, preve; public : mcf(){ } mcf(long long V): V(V){ init(V); } void init(long long MAX_V){ V = MAX_V; G.assign(V, {}); } void add_edge(long long from, long long to, long long cap, long long cost){ G[from].push_back((edge){to, cap, cost, (long long)G[to].size()}); G[to].push_back((edge){from, 0, -cost, (long long)G[from].size()-1}); } long long min_cost_flow(long long s, long long t, long long f) { long long res = 0; prevv.assign(V, 0); preve.assign(V, 0); h.assign(V, 0); while(f > 0){ priority_queue, vector>, greater>> que; dist.assign(V, INF); dist[s] = 0; que.emplace(0, s); while(!que.empty()){ pair p = que.top(); que.pop(); long long v = p.second; if(dist[v] < p.first) continue; for(long long i = 0; i < G[v].size(); i++){ edge &e = G[v][i]; if(e.cap > 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; que.emplace(dist[e.to], e.to); } } } if(dist[t] == INF) { return -1; } for(long long v = 0; v < V; v++) h[v] += dist[v]; long long d = f; for(long long v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for(long long v = t; v != s; v = prevv[v]){ edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } }; signed main(){ cout<>N>>M; m.init(N); for(int i = 0; i < M; i++){ int u, v, c, d; cin>>u>>v>>c>>d; u--, v--; m.add_edge(u, v, 1, c); m.add_edge(u, v, 1, d); } cout<