#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e9;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int to[N << 1], ww[N << 1], cost[N << 1], nxt[N << 1], h[N],
    tt = 1, ans, s, t, n, m, dis[N], cur[N];
bool vis[N];
void adde(int u, int v, int w, int c) {
  to[++tt] = v, ww[tt] = w, cost[tt] = c, nxt[tt] = h[u], h[u] = tt;
  to[++tt] = u, ww[tt] = 0, cost[tt] = -c, nxt[tt] = h[v], h[v] = tt;
}
bool spfa() {
  memset(dis, 0x3f, 4 * (t + 1));
  memcpy(cur, h, 4 * (t + 1));
  deque<int> q;
  q.push_back(s);
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop_front();
    vis[u] = 0;
    for (int i = h[u], v; v = to[i], i; i = nxt[i])
      if (ww[i] && dis[v] > dis[u] + cost[i]) {
        dis[v] = dis[u] + cost[i];
        if (!vis[v]) vis[v] = 1, cost[i] < 0 ? q.push_front(v) : q.push_back(v);
      }
  }
  return dis[t] < inf;
}
int dfs(int u, int flow) {
  if (u == t) return flow;
  vis[u] = 1;
  int ret = 0;
  for (int &i = cur[u], v; v = to[i], i; i = nxt[i])
    if (ww[i] && dis[v] == dis[u] + cost[i] && !vis[v]) {
      int d = dfs(v, min(flow, ww[i]));
      ww[i] -= d, ww[i ^ 1] += d, ret += d, flow -= d, ans += cost[i] * d;
      if (!flow) {
        vis[u] = 0;
        return ret;
      }
    }
  dis[u] = inf;
  vis[u] = 0;
  return ret;
}
int main() {
  cin >> n >> m;
  s = 0, t = n + 1;
  for (int i = 1; i <= m; i++) {
    int u = gi(), v = gi(), c = gi(), f = gi();
    adde(s, v, f, 0);
    adde(u, t, f, 0);
    if (f <= c)
      adde(u, v, c - f, 1), adde(u, v, inf, 2), adde(v, u, f, 1);
    else
      ans += f - c, adde(u, v, inf, 2), adde(v, u, f - c, 0), adde(v, u, c, 1);
  }
  adde(n, 1, inf, 0);
  while (spfa()) dfs(s, inf);
  cout << ans;
  return 0;
}
