#include <bits/stdc++.h>
using namespace std;
const int N = 105, INF = 1e8;
int n, m, k = 1, s, t, u, v, c, f, ans;
int head[N], dis[N], pre[N], deg[N], flow[N], last[N];
bool vis[N];
struct edge {
  int v, f, w, next;
} e[N << 5];
queue<int> q;
inline void add(int u, int v, int f, int w) {
  e[++k] = (edge){v, f, w, head[u]};
  head[u] = k;
  e[++k] = (edge){u, 0, -w, head[v]};
  head[v] = k;
}
inline bool SPFA(void) {
  memset(dis, 0x3f, sizeof(dis));
  memset(flow, 0x3f, sizeof(flow));
  pre[t] = -1, dis[s] = 0;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (register int i = head[u]; i; i = e[i].next)
      if (e[i].f && dis[e[i].v] > dis[u] + e[i].w) {
        dis[e[i].v] = dis[u] + e[i].w;
        flow[e[i].v] = ((flow[u]) < (e[i].f) ? (flow[u]) : (e[i].f));
        pre[e[i].v] = u, last[e[i].v] = i;
        if (!vis[e[i].v]) q.push(e[i].v);
        vis[e[i].v] = true;
      }
  }
  return ~pre[t];
}
inline int MCMF(void) {
  int res = 0;
  while (SPFA()) {
    int u = t;
    res += dis[u] * flow[u];
    while (u != s) {
      e[last[u]].f -= flow[t];
      e[last[u] ^ 1].f += flow[t];
      u = pre[u];
    }
  }
  return res;
}
int main(void) {
  scanf("%d%d", &n, &m);
  s = 0, t = n + 1;
  while (m--) {
    scanf("%d%d%d%d", &u, &v, &c, &f);
    deg[u] += f, deg[v] -= f;
    if (c >= f) {
      add(v, u, f, 1);
      add(u, v, c - f, 1);
      add(u, v, INF, 2);
    } else {
      ans += f - c;
      add(v, u, f - c, 0);
      add(v, u, c, 1);
      add(u, v, INF, 2);
    }
  }
  for (register int i = 1; i <= n; ++i)
    if (deg[i] > 0)
      add(i, t, deg[i], 0);
    else if (deg[i] < 0)
      add(s, i, -deg[i], 0);
  add(n, 1, INF, 0);
  ans += MCMF();
  printf("%d\n", ans);
  return 0;
}
