#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T, class U>
inline void Max(T &a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void Min(T &a, U b) {
  if (a > b) a = b;
}
inline void add(int &a, int b) {
  a += b;
  while (a >= 1000000007) a -= 1000000007;
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * (long long)a % 1000000007;
    a = (long long)a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
struct node {
  int v, c, w, next;
} g[100010];
int head[100010], cnt, p[110], d[110], de[110], vis[110];
void add(int a, int b, int c, int w) {
  g[cnt] = {b, c, w, head[a]}, head[a] = cnt++;
  g[cnt] = {a, 0, -w, head[b]}, head[b] = cnt++;
}
bool bfs(int s, int t) {
  for (int i = 0; i < t + 1; i++) d[i] = -1, vis[i] = 0;
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = g[i].next) {
      int j = g[i].v;
      if (g[i].c && (d[j] == -1 || d[j] > d[u] + g[i].w)) {
        p[j] = i;
        d[j] = d[u] + g[i].w;
        if (!vis[j]) vis[j] = 1, q.push(j);
      }
    }
  }
  return d[t] != -1;
}
long long max_flow(int s, int t) {
  long long ans = 0;
  while (bfs(s, t)) {
    int first = t, f = 1000000007;
    while (first != s) {
      Min(f, g[p[first]].c), first = g[p[first] ^ 1].v;
    }
    ans += d[t] * (long long)f;
    first = t;
    while (first != s) {
      g[p[first]].c -= f, g[p[first] ^ 1].c += f;
      first = g[p[first] ^ 1].v;
    }
  }
  return ans;
}
int main() {
  int i, j, k, K, T, n, m;
  scanf("%d%d", &n, &m);
  int s = 0, t = n + 1;
  memset(head, -1, sizeof(head));
  add(n, 1, 1000000007, 0);
  long long ans = 0;
  while (m--) {
    int u, v, f, c;
    scanf("%d%d%d%d", &u, &v, &c, &f);
    de[u] -= f, de[v] += f;
    if (f > c) {
      ans += f - c;
      add(u, v, 1000000007, 2);
      add(v, u, f - c, 0);
      add(v, u, c, 1);
    } else {
      add(u, v, c - f, 1);
      add(u, v, 1000000007, 2);
      add(v, u, f, 1);
    }
  }
  for (int i = 1; i < t; i++) {
    if (de[i] > 0)
      add(s, i, de[i], 0);
    else
      add(i, t, -de[i], 0);
  }
  ans += max_flow(s, t);
  printf("%I64d\n", ans);
  return 0;
}
