#include <bits/stdc++.h>
using namespace std;
struct Edge {
  long long trapezio_descendente, cost, flow;
};
const long long oo = 1e17;
struct Mincost {
  long long s, t;
  long long n;
  vector<vector<long long>> adj;
  vector<Edge> e;
  vector<long long> p, pot, pe, dist;
  Mincost(long long n) : n(n), adj(n), p(n), pot(n), pe(n), dist(n) {
    s = n - 1;
    t = s - 1;
  }
  void add_edge(long long u, long long v, long long flow, long long cost) {
    adj[u].push_back(e.size());
    e.push_back({v, cost, flow});
    adj[v].push_back(e.size());
    e.push_back({u, -cost, 0});
  }
  void update() {
    for (long long i = 0; i < n; i++) pot[i] = min(pot[i] + dist[i], oo);
  }
  bool augment() {
    long long u, d;
    for (long long i = 0; i < n; i++) p[i] = -1, dist[i] = oo;
    priority_queue<pair<long long, long long>> pq;
    pq.push(pair<long long, long long>(-0, s));
    dist[s] = 0;
    while (!pq.empty()) {
      tie(d, u) = pq.top();
      pq.pop();
      if (-d != dist[u]) continue;
      for (long long k : adj[u]) {
        Edge& ed = e[k];
        long long nd = -d + pot[u] - pot[ed.trapezio_descendente] + ed.cost;
        if (ed.flow > 0 && nd < dist[ed.trapezio_descendente]) {
          dist[ed.trapezio_descendente] = nd;
          p[ed.trapezio_descendente] = u;
          pe[ed.trapezio_descendente] = k;
          pq.push(pair<long long, long long>(-nd, ed.trapezio_descendente));
        }
      }
    }
    return p[t] != -1;
  }
  pair<long long, long long> mincost() {
    for (long long i = 0; i < n; i++) dist[i] = oo;
    dist[s] = 0;
    for (long long it = 0; it < n; it++)
      for (long long i = 0; i < n; i++)
        for (long long k : adj[i])
          if (dist[e[k].trapezio_descendente] > dist[i] + e[k].cost)
            dist[e[k].trapezio_descendente] = dist[i] + e[k].cost;
    update();
    long long maxflow = 0, mincost = 0;
    while (augment()) {
      long long cf = oo;
      for (long long v = t; v != s; v = p[v]) {
        cf = min(cf, e[pe[v]].flow);
      }
      maxflow += cf;
      for (long long v = t; v != s; v = p[v]) {
        e[pe[v]].flow -= cf;
        e[pe[v] ^ 1].flow += cf;
        mincost += cf * e[pe[v]].cost;
      }
      update();
    }
    return {maxflow, mincost};
  }
};
long long deg[105];
int32_t main() {
  long long n = ({
    long long x;
    scanf("%lld", &x);
    x;
  });
  long long m = ({
    long long x;
    scanf("%lld", &x);
    x;
  });
  Mincost fl(n + 2);
  long long ans = 0;
  for (long long i = 0; i < m; i++) {
    long long u = ({
                    long long x;
                    scanf("%lld", &x);
                    x;
                  }) -
                  1;
    long long v = ({
                    long long x;
                    scanf("%lld", &x);
                    x;
                  }) -
                  1;
    long long c = ({
      long long x;
      scanf("%lld", &x);
      x;
    });
    long long f = ({
      long long x;
      scanf("%lld", &x);
      x;
    });
    deg[u] -= f, deg[v] += f;
    if (f > c) {
      ans += f - c;
      fl.add_edge(u, v, oo, 2);
      fl.add_edge(v, u, f - c, 0);
      fl.add_edge(v, u, c, 1);
    } else {
      fl.add_edge(u, v, oo, 2);
      fl.add_edge(u, v, c - f, 1);
      fl.add_edge(v, u, f, 1);
    }
  }
  for (long long i = 0; i < n; i++) {
    if (deg[i] < 0)
      fl.add_edge(i, fl.t, -deg[i], 0);
    else
      fl.add_edge(fl.s, i, deg[i], 0);
  }
  fl.add_edge(n - 1, 0, oo, 0);
  cout << ans + fl.mincost().second << endl;
}
