#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n, m, rem, ans, p[102], dis[102], sink, src, sum[102];
struct edge {
  int u, v, c, w;
} e[N];
void addEdge(int u, int v, int c, int w) {
  e[rem].u = u;
  e[rem].v = v;
  e[rem].c = c;
  e[rem++].w = w;
  e[rem].u = v;
  e[rem].v = u;
  e[rem].c = 0;
  e[rem++].w = -w;
}
bool FindPath() {
  for (int i = 0; i < n + 2; i++) dis[i] = 1e9, p[i] = -1;
  p[src] = dis[src] = 0;
  for (int i = 0; i < n + 1; i++)
    for (int j = 0; j < rem; j++)
      if (e[j].c && p[e[j].u] != -1 && dis[e[j].u] + e[j].w < dis[e[j].v]) {
        dis[e[j].v] = dis[e[j].u] + e[j].w;
        p[e[j].v] = j;
      }
  return p[sink] != -1;
}
int MinCostMaxFlow() {
  int cost = 0;
  while (FindPath()) {
    int mn = 1e9;
    for (int cur = sink; cur != src; cur = e[p[cur]].u)
      mn = min(mn, e[p[cur]].c);
    for (int cur = sink; cur != src; cur = e[p[cur]].u) {
      e[p[cur]].c -= mn;
      e[p[cur] ^ 1].c += mn;
    }
    cost += mn * dis[sink];
  }
  return cost;
}
int main() {
  scanf("%d%d", &n, &m);
  src = n;
  sink = n + 1;
  for (int i = 0, u, v, c, f; i < m; i++) {
    scanf("%d%d%d%d", &u, &v, &c, &f);
    --u;
    --v;
    sum[u] -= f;
    sum[v] += f;
    if (c >= f) {
      addEdge(v, u, f, 1);
      if (c - f) addEdge(u, v, c - f, 1);
      addEdge(u, v, 1e9, 2);
    } else {
      ans += f - c;
      addEdge(u, v, 1e9, 2);
      addEdge(v, u, c, 1);
      addEdge(v, u, f - c, 0);
    }
  }
  addEdge(n - 1, 0, 1e9, 0);
  for (int i = 0; i < n; i++)
    if (sum[i] > 0)
      addEdge(src, i, sum[i], 0);
    else if (sum[i] < 0)
      addEdge(i, sink, -sum[i], 0);
  printf("%d\n", ans + MinCostMaxFlow());
}
