#include <bits/stdc++.h>   using namespace std;   constexpr long long mod = 1000000007;   const long long INF = mod * mod;   const long double eps = 1e-12;   const long double pi = acosl(-1.0);   long long mod_pow(long long x, long long n, long long m = mod) {     long long res = 1;     while (n) {       if (n & 1) res = res * x % m;       x = x * x % m;       n >>= 1;     }     return res;   }   struct modint {     long long n;     modint() : n(0) { ; }     modint(long long m) : n(m) {       if (n >= mod)         n %= mod;       else if (n < 0)         n = (n % mod + mod) % mod;     }     operator int() { return n; }   };   bool operator==(modint a, modint b) { return a.n == b.n; }   modint operator+=(modint& a, modint b) {     a.n += b.n;     if (a.n >= mod) a.n -= mod;     return a;   }   modint operator-=(modint& a, modint b) {     a.n -= b.n;     if (a.n < 0) a.n += mod;     return a;   }   modint operator*=(modint& a, modint b) {     a.n = ((long long)a.n * b.n) % mod;     return a;   }   modint operator+(modint a, modint b) { return a += b; }   modint operator-(modint a, modint b) { return a -= b; }   modint operator*(modint a, modint b) { return a *= b; }   modint operator^(modint a, long long n) {     if (n == 0) return modint(1);     modint res = (a * a) ^ (n / 2);     if (n % 2) res = res * a;     return res;   }   long long inv(long long a, long long p) {     return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);   }   modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }   int max_n;   const int mn = 100000;   struct edge {     int to, cap;     long long cost;     int rev;   };   vector<edge> G[mn];   pair<int, int> par[mn];   long long dist[mn];   void add_edge(int from, int to, int cap, long long cost) {     G[from].push_back({to, cap, cost, (int)G[to].size()});     G[to].push_back({from, 0, -cost, (int)G[from].size() - 1});     max_n = max({max_n, from + 1, to + 1});   }   void add_edge2(int from, int to, int cap, long long cost) {     G[from].push_back({to, cap, cost, -1});     max_n = max({max_n, from + 1, to + 1});   }   pair<long long, long long> minimum_road(int s, int t) {     fill(par, par + max_n, pair<int, int>{-1, -1});     fill(dist, dist + max_n, INF);     dist[s] = 0;     priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                    greater<pair<long long, long long>>>         q;     q.push({0, s});     while (!q.empty()) {       pair<long long, long long> p = q.top();       q.pop();       int id = p.second;       if (id == t) continue;       if (p.first > dist[id]) continue;       for (int j = 0; j < G[id].size(); j++) {         if (G[id][j].cap > 0) {           int to = G[id][j].to;           long long nd = p.first + G[id][j].cost;           if (nd < dist[to]) {             dist[to] = nd;             par[to] = {id, j};             q.push({dist[to], to});           }         }       }     }     int cur = t;     int f = mod;     while (cur != s) {       int p = par[cur].first, j = par[cur].second;       if (p < 0) return {-1, -1};       f = min(f, G[p][j].cap);       cur = p;     }     cur = t;     while (cur != s) {       int p = par[cur].first, j = par[cur].second;       if (p < 0) return {-1, -1};       G[p][j].cap -= f;       if (G[p][j].rev >= 0) {         G[cur][G[p][j].rev].cap += f;       }       cur = p;     }     return {dist[t], f};   }   long long minimum_cost_flow(int s, int t, int k = mod) {     long long ret = 0;     for (int i = 0; i < k; i++) {       pair<long long, long long> z = minimum_road(s, t);       if (z.first < 0) return ret;       if (k - i <= z.second) {         ret += z.first * (k - i);         break;       }       i += z.second - 1;       ret += z.first * z.second;     }     return ret;   }   void solve() {     int n, m;     cin >> n >> m;     vector<int> d(n + 2 * m);     for (int i = 0; i < m; i++) {       int a, b, c, f;       cin >> a >> b >> c >> f;       a--;       b--;       int le = n + 2 * i;       int ri = n + 2 * i + 1;       add_edge(a, le, mod, 1);       add_edge(le, a, f, 1);       add_edge(ri, b, mod, 1);       add_edge(b, ri, f, 1);       add_edge(le, ri, c, 0);       add_edge(le, ri, mod, 2);       d[a] += -f;       d[le] += f;       d[ri] += -f;       d[b] += f;     }     int sum = 0;     int s = n + 2 * m;     int t = s + 1;     int sum2 = 0;     int S = t + 1;     int T = S + 1;     for (int i = 0; i < n + 2 * m; i++) {       if (d[i] > 0) {         sum += abs(d[i]);         add_edge(s, i, d[i], 0);       } else if (d[i] < 0) {         sum2 += abs(d[i]);         add_edge(i, t, -d[i], 0);       }     }     max_n = T + 1;     add_edge(n - 1, 0, mod, 0);     int ans = 0;     ans += minimum_cost_flow(s, t, sum);     cout << ans / 2 <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }