#include <bits/stdc++.h>
inline int nextChar(void) {
  const int siz = 1024;
  static char buf[siz];
  static char *hd = buf + siz;
  static char *tl = buf + siz;
  if (hd == tl) fread(hd = buf, 1, siz, stdin);
  return *hd++;
}
inline int nextInt(void) {
  register int ret = 0;
  register int neg = false;
  register int bit = nextChar();
  for (; bit < 48; bit = nextChar())
    if (bit == '-') neg ^= true;
  for (; bit > 47; bit = nextChar()) ret = ret * 10 + bit - 48;
  return neg ? -ret : ret;
}
const int siz = 1005;
const int inf = 1000000007;
int n, m;
int answer;
int del[siz];
int tot;
int s, t;
int hd[siz];
int to[siz];
int fl[siz];
int vl[siz];
int nt[siz];
inline void add(int u, int v, int f, int w) {
  nt[tot] = hd[u];
  to[tot] = v;
  fl[tot] = f;
  vl[tot] = +w;
  hd[u] = tot++;
  nt[tot] = hd[v];
  to[tot] = u;
  fl[tot] = 0;
  vl[tot] = -w;
  hd[v] = tot++;
}
int dep[siz];
inline bool bfs(void) {
  static int que[siz], head, tail;
  for (int i = s; i <= t; ++i) dep[i] = 0;
  dep[que[head = 0] = s] = tail = 1;
  while (head != tail) {
    int u = que[head++], v;
    for (int i = hd[u]; ~i; i = nt[i])
      if (!dep[v = to[i]] && fl[i] && !vl[i]) dep[que[tail++] = v] = dep[u] + 1;
  }
  return dep[t];
}
int cur[siz];
inline int min(int a, int b) { return a < b ? a : b; }
int dfs(int u, int f) {
  if (u == t || !f) return f;
  int used = 0, flow, v;
  for (int i = hd[u]; ~i; i = nt[i])
    if (dep[v = to[i]] == dep[u] + 1 && fl[i] && !vl[i]) {
      flow = dfs(v, min(fl[i], f - used));
      used += flow;
      fl[i] -= flow;
      fl[i ^ 1] += flow;
      if (used == f) return f;
      if (fl[i]) cur[u] = i;
    }
  if (!used) dep[u] = 0;
  return used;
}
inline void maxFlow(void) {
  while (bfs())
    while (dfs(s, inf))
      ;
}
int dis[siz];
int pre[siz];
inline bool spfa(void) {
  static int que[siz];
  static int inq[siz];
  static int head, tail;
  for (int i = s; i <= t; ++i) inq[i] = 0, dis[i] = inf;
  head = 0, tail = 0;
  que[tail++] = s;
  pre[s] = -1;
  dis[s] = 0;
  while (head != tail) {
    int u = que[head++], v;
    inq[u] = 0;
    for (int i = hd[u]; ~i; i = nt[i])
      if (dis[v = to[i]] > dis[u] + vl[i] && fl[i]) {
        dis[v] = dis[u] + vl[i], pre[v] = i ^ 1;
        if (!inq[v]) inq[que[tail++] = v] = 1;
      }
  }
  return dis[t] < inf;
}
inline void minCost(void) {
  while (spfa()) {
    int flow = inf;
    for (int i = pre[t]; ~i; i = pre[to[i]])
      if (fl[i ^ 1] < flow) flow = fl[i ^ 1];
    for (int i = pre[t]; ~i; i = pre[to[i]]) fl[i] += flow, fl[i ^ 1] -= flow;
    answer += dis[t] * flow;
  }
}
int main(void) {
  n = nextInt();
  m = nextInt();
  s = 0, t = n + 1;
  for (int i = s; i <= t; ++i) hd[i] = -1;
  for (int i = 1; i <= m; ++i) {
    int u = nextInt();
    int v = nextInt();
    int c = nextInt();
    int f = nextInt();
    del[u] += f;
    del[v] -= f;
    if (f <= c) {
      add(u, v, c - f, 1);
      add(u, v, inf, 2);
      add(v, u, f, 1);
    } else {
      answer += f - c;
      add(u, v, inf, 2);
      add(v, u, f - c, 0);
      add(v, u, c, 1);
    }
  }
  add(n, 1, inf, 0);
  for (int i = 1; i <= n; ++i)
    if (del[i] < 0)
      add(s, i, -del[i], 0);
    else if (del[i] > 0)
      add(i, t, del[i], 0);
  maxFlow();
  minCost();
  printf("%d\n", answer);
  return fclose(stdin), fclose(stdout), 0;
}
