#include <bits/stdc++.h>
inline int min(int x, int y) { return x < y ? x : y; }
struct edge {
  int u, v, w, c;
  edge *e, *t;
} GE[(100 + 100 * 3 + 1) * 2], *CE = GE, *GA[(100 + 2)], *pre[(100 + 2)];
inline void edge_new(int u, int v, int w, int c) {
  *CE = (edge){u, v, +w, c, GA[u], CE + 1}, GA[u] = CE++;
  *CE = (edge){v, u, -w, 0, GA[v], CE - 1}, GA[v] = CE++;
}
int V, dis[(100 + 2)], Ql, Qr, Qn, Q[(100 + 2)];
bool inQ[(100 + 2)];
inline int spfa(int s, int t) {
  memset(dis, 0x7F7F7F7F, V * sizeof(int));
  dis[s] = 0, inQ[Q[Qr] = s] = true, ++Qn, Qr = Qr < (100 + 2) - 1 ? Qr + 1 : 0;
  register int u, v, d;
  register edge* e;
  do
    for (e = GA[u = Q[Ql]], inQ[Q[Ql]] = false, --Qn,
        Ql = Ql < (100 + 2) - 1 ? Ql + 1 : 0;
         e; e = e->e)
      if (e->c && dis[v = e->v] > (d = dis[u] + e->w))
        dis[v] = inQ[v] ? d
                        : (inQ[Q[Qr] = v] = true, ++Qn,
                           Qr = Qr < (100 + 2) - 1 ? Qr + 1 : 0, d),
        pre[v] = e;
  while (Qn);
  if (dis[t] == 0x7F7F7F7F) return 0;
  d = 0x7F7F7F7F;
  for (e = pre[t]; e; e = pre[e->u]) d = min(d, e->c);
  for (e = pre[t]; e; e = pre[e->u]) e->c -= d, e->t->c += d;
  return d;
}
inline int Mfmw(int s, int t) {
  register int w = 0, df;
  while ((df = spfa(s, t))) w += df * dis[t];
  return w;
}
int n, m, s, t, u, v, c, f, d[100 + 1], ans;
int main() {
  scanf("%d %d", &n, &m);
  s = 0, t = n + 1, V = t + 1;
  edge_new(n, 1, 0, 0x7F7F7F7F);
  while (m--) {
    scanf("%d %d %d %d", &u, &v, &c, &f);
    d[u] -= f, d[v] += f;
    edge_new(u, v, 2, 0x7F7F7F7F);
    edge_new(v, u, 1, min(c, f));
    if (c < f)
      edge_new(v, u, 0, f - c), ans += f - c;
    else if (c > f)
      edge_new(u, v, 1, c - f);
  }
  for (u = 1; u <= n; ++u)
    if (d[u] > 0)
      edge_new(s, u, 0, +d[u]);
    else if (d[u] < 0)
      edge_new(u, t, 0, -d[u]);
  printf("%d\n", ans + Mfmw(s, t));
  return 0;
}
