#include <bits/stdc++.h>
using namespace std;
const int N = 100100, inf = 1061109567;
struct node {
  int to, flow, w, nxt;
} h[N];
int n, m, s, t, ans, tot, head[N], dis[N], du[N];
bool vis[N], vs[N];
int read() {
  int aa = 0, bb = 1;
  char cc = getchar();
  while (cc > '9' || cc < '0') {
    if (cc == '-') bb = -1;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    aa = (aa << 3) + (aa << 1) + (cc ^ '0');
    cc = getchar();
  }
  return aa * bb;
}
void add(int x, int y, int flow, int w) {
  h[++tot].to = y;
  h[tot].flow = flow;
  h[tot].w = w;
  h[tot].nxt = head[x];
  head[x] = tot;
}
void link(int x, int y, int flow, int w) {
  add(x, y, flow, w);
  add(y, x, 0, -w);
}
void Link(int x, int y, int l, int r, int w) {
  du[x] -= l;
  du[y] += l;
  link(x, y, r - l, w);
}
bool spfa() {
  queue<int> q;
  memset(dis, 0x3f, sizeof(dis));
  memset(vs, 0, sizeof(vs));
  dis[s] = 0;
  vs[s] = 1;
  q.push(s);
  while (q.size()) {
    int x = q.front();
    q.pop();
    vs[x] = 0;
    for (int i = head[x]; i; i = h[i].nxt) {
      int y = h[i].to;
      if (h[i].flow > 0 && dis[y] > dis[x] + h[i].w) {
        dis[y] = dis[x] + h[i].w;
        if (!vs[y]) vs[y] = 1, q.push(y);
      }
    }
  }
  return dis[t] < inf;
}
int dfs(int x, int flow) {
  if (x == t || flow <= 0) return flow;
  int res = flow;
  vis[x] = 1;
  for (int i = head[x]; i; i = h[i].nxt) {
    int y = h[i].to;
    if (h[i].flow <= 0 || vis[y] || dis[y] != dis[x] + h[i].w) continue;
    int tmp = dfs(y, min(res, h[i].flow));
    if (tmp <= 0) dis[y] = -1;
    h[i].flow -= tmp;
    h[i ^ 1].flow += tmp;
    res -= tmp;
    if (res <= 0) break;
  }
  vis[x] = 0;
  return flow - res;
}
void zkw() {
  while (spfa()) ans += dis[t] * dfs(s, inf);
}
int main() {
  n = read();
  m = read();
  s = n + 1;
  t = n + 2;
  tot = 1;
  link(n, 1, inf, 0);
  int u, v, c, f;
  for (int i = 1; i <= m; i++) {
    u = read();
    v = read();
    c = read();
    f = read();
    if (c >= f) {
      Link(v, u, 0, f, 1);
      Link(u, v, 0, c - f, 1);
      Link(u, v, 0, inf, 2);
    } else {
      ans += f - c;
      Link(v, u, 0, c, 1);
      Link(v, u, 0, f - c, 0);
      Link(u, v, 0, inf, 2);
    }
    Link(u, v, f, f, 0);
  }
  for (int i = 1; i <= n; i++) {
    if (du[i] > 0) link(s, i, du[i], 0);
    if (du[i] < 0) link(i, t, -du[i], 0);
  }
  zkw();
  printf("%d\n", ans);
  return 0;
}
