#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, nxt, flow, w;
} e[500050];
int head[205], ecnt;
long long dep[205];
bool inq[205];
int n, m;
int s, t;
const long long inf = 1e18;
void adde(int u, int v, int fl, int w) {
  e[ecnt].v = v;
  e[ecnt].nxt = head[u];
  e[ecnt].flow = fl;
  e[ecnt].w = w;
  head[u] = ecnt++;
}
void Adde(int u, int v, int fl, int w) {
  adde(u, v, fl, w);
  adde(v, u, 0, -w);
}
bool spfa() {
  deque<long long> que;
  for (int i = 0; i <= t; ++i) dep[i] = (1ll << 62);
  memset(inq, 0, sizeof(inq));
  dep[t] = 0;
  que.push_back(t);
  while (!que.empty()) {
    long long u = que.front();
    que.pop_front();
    for (long long i = head[u]; ~i; i = e[i].nxt) {
      long long v = e[i].v;
      if (e[i ^ 1].flow && dep[u] - e[i].w < dep[v]) {
        dep[v] = dep[u] + e[i ^ 1].w;
        if (!inq[v]) {
          if (que.empty() || dep[v] < dep[que.front()])
            que.push_front(v);
          else
            que.push_back(v);
          inq[v] = true;
        }
      }
    }
    inq[u] = false;
  }
  return dep[s] < (1ll << 62);
}
int curedge[205];
long long ans;
bool vis[205];
long long dfs(long long s, long long t, long long flow) {
  if (s == t) return flow;
  long long delta = flow;
  vis[s] = true;
  for (long long i = head[s]; ~i; i = e[i].nxt) {
    long long v = e[i].v;
    if (!vis[v] && dep[v] == dep[s] - e[i].w && e[i].flow) {
      long long g = dfs(v, t, min((long long)e[i].flow, delta));
      delta -= g;
      ans += g * e[i].w;
      e[i].flow -= g;
      e[i ^ 1].flow += g;
      if (!delta) return flow;
    }
  }
  return flow - delta;
}
long long dinic() {
  long long anss = 0;
  while (spfa()) {
    vis[t] = 1;
    while (vis[t]) {
      memset(vis, 0, sizeof(vis));
      anss += dfs(s, t, inf);
    }
  }
  return anss;
}
int pre[205];
int fs[205];
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d%d", &n, &m);
  s = 0, t = n + 1;
  int cos = 0;
  for (int i = 1; i <= m; ++i) {
    int u, v, c, f;
    scanf("%d%d%d%d", &u, &v, &c, &f);
    fs[u] -= f;
    fs[v] += f;
    if (c > f) {
      Adde(v, u, f, 1);
      Adde(u, v, c - f, 1);
      Adde(u, v, 0x3f3f3f3f, 2);
    } else {
      cos += f - c;
      Adde(u, v, 0x3f3f3f3f, 2);
      Adde(v, u, f - c, 0);
      Adde(v, u, c, 1);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (fs[i] > 0)
      Adde(s, i, fs[i], 0);
    else
      Adde(i, t, -fs[i], 0);
  Adde(n, 1, 0x3f3f3f3f, 0);
  dinic();
  printf("%lld\n", ans + cos);
}
