#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  x *= f;
}
struct edge {
  int u, v, cp, w, next;
} e[2000005 << 1];
int head[205], k;
inline void add(int u, int v, int cp, int w) {
  e[k] = (edge){u, v, cp, w, head[u]};
  head[u] = k++;
  e[k] = (edge){v, u, 0, -w, head[v]};
  head[v] = k++;
}
int n, m, mincost, maxflow;
int dis[205], nxt[205];
bool vis[205];
inline bool SPFA(int s, int t) {
  queue<int> q;
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[s] = 0;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    nxt[u] = head[u];
    vis[u] = false;
    for (int i = head[u]; ~i; i = e[i].next) {
      int v = e[i].v;
      if (!e[i].cp) continue;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        if (!vis[v]) q.push(v), vis[v] = true;
      }
    }
  }
  return dis[t] != 0x3f3f3f3f;
}
inline int Dinic(int u, int t, int flow) {
  if (u == t || !flow) return flow;
  int rest = flow;
  vis[u] = true;
  for (int &i = nxt[u]; ~i; i = e[i].next) {
    int v = e[i].v;
    if (!e[i].v || dis[v] != dis[u] + e[i].w || vis[v]) continue;
    int k = Dinic(v, t, min(rest, e[i].cp));
    if (!k) continue;
    mincost += k * e[i].w;
    e[i].cp -= k;
    e[i ^ 1].cp += k;
    rest -= k;
    if (!rest) break;
  }
  vis[u] = false;
  return flow - rest;
}
int d[205];
int main() {
  read(n), read(m);
  memset(head, -1, sizeof(head));
  int s = n + 1, t = s + 1;
  for (int i = 1, u, v, c, f; i <= m; ++i) {
    read(u), read(v), read(c), read(f);
    d[u] += f;
    d[v] -= f;
    if (f <= c) {
      add(u, v, c - f, 1);
      add(v, u, f, 1);
      add(u, v, 0x3f3f3f3f, 2);
    } else {
      mincost += f - c;
      add(v, u, f - c, 0);
      add(v, u, c, 1);
      add(u, v, 0x3f3f3f3f, 2);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (d[i] > 0)
      add(i, t, d[i], 0);
    else
      add(s, i, -d[i], 0);
  add(n, 1, 0x3f3f3f3f, 0);
  while (SPFA(s, t)) maxflow += Dinic(s, t, 1 << 30);
  printf("%d\n", mincost);
  return 0;
}
