#include <bits/stdc++.h>
using namespace std;
int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
struct Edge {
  int to, next, v, c;
} a[40001 << 7];
int h[40001], cnt = 1;
void add(int x, int y, int z, int w) {
  a[++cnt] = (Edge){y, h[x], z, w}, h[x] = cnt,
  a[++cnt] = (Edge){x, h[y], 0, -w}, h[y] = cnt;
}
int S, T, n, m, p[40001], d[40001];
int flow, val;
bool v[40001];
bool Spfa() {
  std::queue<int> q;
  memset(v, 0, sizeof(v));
  memset(p, 0, sizeof(p));
  memset(d, 0x3f, sizeof(d));
  q.push(S), d[S] = 0, v[S] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    v[x] = 0;
    for (int i = h[x]; i; i = a[i].next)
      if (a[i].v && d[x] + a[i].c < d[a[i].to]) {
        d[a[i].to] = d[x] + a[i].c, p[a[i].to] = i;
        if (!v[a[i].to]) v[a[i].to] = 1, q.push(a[i].to);
      }
  }
  if (d[T] == 1061109567) return 0;
  return 1;
}
void Adjust() {
  int minf = 0x7fffffff, x = T;
  for (int i = T; i != S; i = a[p[i] ^ 1].to) minf = min(minf, a[p[i]].v);
  for (int i = T; i != S; i = a[p[i] ^ 1].to)
    a[p[i]].v -= minf, a[p[i] ^ 1].v += minf;
  flow += minf, val += d[T] * minf;
}
int Edmonds_Karp() {
  flow = val = 0;
  while (Spfa()) Adjust();
  return val;
}
int du[110];
signed main() {
  int x, y, c, f, ans = 0;
  n = read(), m = read(), add(n, 1, 0x7fffffff, 0), S = n + 1, T = n + 2;
  for (int i = 1; i <= m; i++) {
    x = read(), y = read(), c = read(), f = read();
    if (c < f)
      ans += f - c, add(y, x, f - c, 0), add(y, x, c, 1),
          add(x, y, 0x7fffffff, 2);
    else
      add(x, y, c - f, 1), add(x, y, 0x7fffffff, 2), add(y, x, f, 1);
    du[x] -= f, du[y] += f;
  }
  for (int i = 1; i <= n; i++)
    if (du[i] > 0)
      add(S, i, du[i], 0);
    else
      add(i, T, -du[i], 0);
  cout << ans + Edmonds_Karp();
  return 0;
}
