#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  bool flag = 0;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48;
  flag ? x = ~x + 1 : 0;
}
int S, T, flow, cost, head[110], tot = 1, dis[110], pre[110];
struct {
  int v, c, w, next;
} e[100001];
queue<int> q;
bool inq[110];
inline void insert(int u, int v, int c, int w) {
  e[++tot].v = v, e[tot].c = c, e[tot].w = w, e[tot].next = head[u],
  head[u] = tot;
}
inline void add(int u, int v, int c, int w) {
  insert(u, v, c, w), insert(v, u, 0, -w);
}
bool spfa() {
  memset(dis, 0x3f, sizeof dis);
  dis[S] = 0, q.push(S);
  while (!q.empty()) {
    int u = q.front();
    q.pop(), inq[u] = 0;
    for (int i = head[u], v; i; i = e[i].next)
      e[i].c &&dis[v = e[i].v] > dis[u] + e[i].w
          ? dis[v] = dis[u] + e[i].w,
            pre[v] = i, (!inq[v] ? q.push(v), inq[v] = 1 : 0) : 0;
  }
  return dis[T] < 0x3f3f3f3f;
}
void mcf() {
  int d = 0x3f3f3f3f;
  for (int i = T; i != S; i = e[pre[i] ^ 1].v) d = min(d, e[pre[i]].c);
  for (int i = T; i != S; i = e[pre[i] ^ 1].v)
    e[pre[i]].c -= d, e[pre[i] ^ 1].c += d, cost += d * e[pre[i]].w;
  flow += d;
}
int d[110];
int main() {
  int n, m;
  read(n), read(m);
  T = n + 1;
  int ans = 0;
  for (int i = (1); i <= (m); i++) {
    int u, v, c, f;
    read(u), read(v), read(c), read(f);
    d[u] -= f, d[v] += f;
    add(u, v, 0x3f3f3f3f, 2);
    if (f >= c)
      ans += f - c, add(v, u, f - c, 0), add(v, u, c, 1);
    else
      add(v, u, f, 1), add(u, v, c - f, 1);
  }
  for (int i = (1); i <= (n); i++)
    d[i] > 0 ? add(S, i, d[i], 0) : add(i, T, -d[i], 0);
  add(n, 1, 0x3f3f3f3f, 0);
  while (spfa()) mcf();
  cout << ans + cost;
  return 0;
}
