#include <bits/stdc++.h>
using namespace std;
const long long RLEN = 1 << 20 | 1;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? EOF : *ib++;
}
inline long long read() {
  char ch = gc();
  long long res = 0;
  bool f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = gc();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline long long readll() {
  char ch = gc();
  long long res = 0;
  bool f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = gc();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = gc();
  return f ? res : -res;
}
inline char readchar() {
  char ch = gc();
  while (isspace(ch)) ch = gc();
  return ch;
}
inline long long readstring(char *s) {
  long long top = 0;
  char ch = gc();
  while (isspace(ch)) ch = gc();
  while (!isspace(ch) && ch != EOF) s[++top] = ch, ch = gc();
  s[top + 1] = '\0';
  return top;
}
template <typename tp>
inline void chemx(tp &a, tp b) {
  a = max(a, b);
}
template <typename tp>
inline void chemn(tp &a, tp b) {
  a = min(a, b);
}
const double eps = 1e-8;
const long long N = 405;
long long n, m, ln, lm;
double f[N][N];
inline void pivot(long long l, long long e) {
  double t = f[l][e];
  for (long long j = 0; j <= lm; j++) f[l][j] /= t;
  for (long long i = 0; i <= ln; i++)
    if (l != i && fabs(f[i][e]) > 0) {
      t = f[i][e], f[i][e] = 0;
      for (long long j = 0; j <= lm; j++) f[i][j] -= t * f[l][j];
    }
}
inline void simplex() {
  while (1) {
    long long l = 0, e = 0;
    double mn = 1e18;
    for (long long i = 1; i <= lm; i++)
      if (f[0][i] > eps) {
        e = i;
        break;
      }
    if (!e) break;
    for (long long i = 1; i <= ln; i++)
      if (f[i][e] > eps && f[i][0] / f[i][e] < mn) {
        mn = f[i][0] / f[i][e], l = i;
      }
    assert(l);
    pivot(l, e);
  }
}
long long u[N], v[N], fl[N], c[N];
long long in[N], out[N];
signed main() {
  n = read(), m = read();
  for (long long i = 1; i <= m; i++) {
    u[i] = read(), v[i] = read(), c[i] = read(), fl[i] = read();
    in[v[i]] += fl[i], out[u[i]] += fl[i];
  }
  ln = 3 * m, lm = 2 * m + 2 * n;
  for (long long i = 1; i <= m; i++)
    f[0][i] = fl[i] - c[i], f[0][i + m] = -fl[i], f[i][i] = 1, f[i + m][i] = -1,
    f[i + 2 * m][i] = 1, f[i + m + m][m + i] = -1;
  for (long long i = 2; i < n; i++)
    f[0][2 * m + i] = out[i] - in[i], f[0][2 * m + n + i] = in[i] - out[i];
  for (long long i = 1; i <= m; i++) {
    if (u[i] > 1) f[i + m][2 * m + u[i]] = -1, f[i + m + m][2 * m + u[i]] = 1;
    if (v[i] < n) f[i + m][2 * m + v[i]] = 1, f[i + m + m][2 * m + v[i]] = -1;
    if (u[i] > 1)
      f[i + m][2 * m + n + u[i]] = 1, f[i + m + m][2 * m + n + u[i]] = -1;
    if (v[i] < n)
      f[i + m][2 * m + n + v[i]] = -1, f[i + m + m][2 * m + n + v[i]] = 1;
  }
  for (long long i = 1; i <= m + m + m; i++) f[i][0] = 1;
  simplex();
  cout << (long long)(-f[0][0] + 0.5) << '\n';
  return 0;
}
