#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
inline int read() {
  register int x = 0, f = 0;
  register char c = getchar();
  while (c < '0' || c > '9') f |= c == '-', c = getchar();
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  return f ? -x : x;
}
int n, m, S, T, cnt = 1;
int head[110], du[110], f[110], dis[110], pre[110];
struct edge {
  int v, nex, c, val;
} e[110000];
inline void add(int u, int v, int c, int val) {
  e[++cnt].v = v;
  e[cnt].nex = head[u];
  e[cnt].c = c;
  e[cnt].val = val;
  head[u] = cnt;
  e[++cnt].v = u;
  e[cnt].nex = head[v];
  e[cnt].c = 0;
  e[cnt].val = -val;
  head[v] = cnt;
}
bool spfa() {
  static int vis[110];
  static queue<int> q;
  for (int i = 1; i <= T; ++i) dis[i] = inf, pre[i] = 0, f[i] = 0, vis[i] = 0;
  dis[S] = 0;
  f[S] = inf;
  q.push(S);
  while (q.size()) {
    int x = q.front();
    q.pop();
    vis[x] = 0;
    for (int i = head[x], v; i; i = e[i].nex) {
      v = e[i].v;
      if (e[i].c && dis[v] > dis[x] + e[i].val) {
        dis[v] = dis[x] + e[i].val;
        pre[v] = i;
        f[v] = min(f[x], e[i].c);
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
    }
  }
  return dis[T] != inf;
}
int calc() {
  int ans = 0;
  while (spfa()) {
    ans += f[T] * dis[T];
    int t = T, i;
    do i = pre[t], e[i].c -= f[T], e[i ^ 1].c += f[T], t = e[i ^ 1].v;
    while (t != S);
  }
  return ans;
}
int main() {
  n = read();
  m = read();
  S = n + 1;
  T = S + 1;
  int u, v, c, f, ans = 0;
  for (int i = 1; i <= m; ++i) {
    u = read();
    v = read();
    c = read();
    f = read();
    if (f > c) {
      ans += f - c;
      add(v, u, f - c, 0);
      add(v, u, c, 1);
    } else {
      add(u, v, c - f, 1);
      add(v, u, f, 1);
    }
    add(u, v, inf, 2);
    du[v] += f;
    du[u] -= f;
  }
  add(n, 1, inf, 0);
  for (int i = 1; i <= n; ++i)
    du[i] > 0 ? add(S, i, du[i], 0) : add(i, T, -du[i], 0);
  cout << ans + calc() << '\n';
  return 0;
}
