#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int INF = 0x3f3f3f3f;
struct Node {
  int u, v, f, c;
  Node *nxt, *tw;
} * g[N], pool[10 * N], *tail = pool;
inline void make_edge(int u, int v, int f, int c) {
  tail->v = v;
  tail->f = f;
  tail->c = c;
  tail->tw = tail + 1;
  tail->nxt = g[u];
  g[u] = tail++;
  tail->v = u;
  tail->f = 0;
  tail->c = -c;
  tail->tw = tail - 1;
  tail->nxt = g[v];
  g[v] = tail++;
}
inline bool ckmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
int n, m;
int ans, cc[N], S, T;
int spfa(int &dt) {
  queue<int> q;
  static int d[N], inq[N], minf[N];
  static Node *cur[N];
  memset(d, 0x3f, sizeof(d));
  minf[S] = INF, d[S] = 0;
  for (q.push(S), inq[S] = 1; !q.empty();) {
    int u = q.front();
    q.pop();
    inq[u] = 0;
    for (Node *p = g[u]; p; p = p->nxt)
      if (p->f > 0 && ckmin(d[p->v], d[u] + p->c))
        minf[p->v] = min(minf[u], p->f), cur[p->v] = p,
        !inq[p->v] ? q.push(p->v), inq[p->v] = 1 : 0;
  }
  if (d[T] == INF || !minf[T]) return 0;
  for (Node *p = cur[T]; p; p = cur[p->tw->v])
    p->f -= minf[T], p->tw->f += minf[T];
  dt = d[T] * minf[T];
  return 1;
}
int costflow() {
  int res = 0, dt;
  while (spfa(dt)) res += dt;
  return res;
}
int main() {
  cin >> n >> m;
  S = n + 1, T = n + 2;
  for (int i = 0; i < m; i++) {
    int u, v, ca, f;
    cin >> u >> v >> ca >> f;
    cc[u] += f;
    cc[v] -= f;
    if (f <= ca) {
      if (f < ca) make_edge(u, v, ca - f, 1);
      make_edge(u, v, INF, 2);
      make_edge(v, u, f, 1);
    } else {
      ans += f - ca;
      make_edge(u, v, INF, 2);
      make_edge(v, u, f - ca, 0);
      make_edge(v, u, ca, 1);
    }
  }
  make_edge(n, 1, INF, 0);
  for (int i = 1; i <= n; i++)
    if (cc[i] > 0)
      make_edge(i, T, cc[i], 0);
    else if (cc[i] < 0)
      make_edge(S, i, -cc[i], 0);
  cout << ans + costflow() << endl;
  return 0;
}
