#include <bits/stdc++.h>
using namespace std;
const int N = 200;
const long long mod = 998244353;
const long long Inf = 1e18;
const long long inf = 1e18;
int head[N], vis[N], pre[N], s, t, cnt, dd[N];
long long cost, flow[N], dis[N];
struct edge {
  long long u, c, d, nxt;
} e[(int)1e6];
void addedge(long long u, long long v, long long c, long long d) {
  e[cnt] = (edge){v, c, d, head[u]}, head[u] = cnt++;
  e[cnt] = (edge){u, 0, -d, head[v]}, head[v] = cnt++;
}
void addlim(long long u, long long v, long long L, long long R, long long d) {
  addedge(u, v, R - L, d);
  dd[u] -= L;
  dd[v] += L;
}
bool bfs() {
  queue<int> q;
  q.push(s);
  for (int i = (0); i < (t + 1); ++i) dis[i] = inf, vis[i] = 0;
  vis[s] = 1;
  dis[s] = 0;
  flow[s] = inf;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = e[i].nxt) {
      int w = e[i].u;
      long long d = e[i].d;
      if (e[i].c > 0 && dis[w] > dis[u] + d) {
        dis[w] = dis[u] + d;
        pre[w] = i;
        flow[w] = min(flow[u], e[i].c);
        if (!vis[w]) {
          vis[w] = 1;
          q.push(w);
        }
      }
    }
  }
  return dis[t] != inf;
}
void costflow() {
  pre[s] = -1;
  while (bfs()) {
    for (int i = pre[t]; ~i; i = pre[e[i ^ 1].u]) {
      e[i].c -= flow[t];
      e[i ^ 1].c += flow[t];
    }
    cost += dis[t] * flow[t];
  }
}
int n, m;
void solve() {
  addedge(n, 1, inf, 0);
  for (int i = (1); i < (N); ++i) {
    if (dd[i] > 0)
      addedge(s, i, dd[i], 0);
    else if (dd[i] < 0)
      addedge(i, t, -dd[i], 0);
  }
  costflow();
}
int main() {
  memset(head, -1, sizeof head);
  scanf("%d%d", &n, &m);
  s = N - 1, t = s - 1;
  long long ans = 0;
  for (int i = (0); i < (m); ++i) {
    int u, v, c, f;
    scanf("%d%d%d%d", &u, &v, &c, &f);
    addlim(u, v, f, f, 0);
    if (c >= f) {
      addedge(u, v, c - f, 1);
      addedge(u, v, Inf, 2);
      addedge(v, u, f, 1);
    } else {
      ans += f - c;
      addedge(v, u, c, 1);
      addedge(v, u, f - c, 0);
      addedge(u, v, Inf, 2);
    }
  }
  solve();
  printf("%lld\n", ans + cost);
}
