#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
const long long INF = 1e9 + 19;
struct Edge {
  int v, f, mf, c;
};
int n, m;
Edge data[N];
vector<int> e[N];
int cur;
int st, fn, all;
int answer;
void addE(int v, int u, int cap, int cost) {
  data[cur] = {u, 0, cap, cost};
  data[cur + 1] = {v, 0, 0, -cost};
  e[v].push_back(cur);
  e[u].push_back(cur + 1);
  cur += 2;
}
int dist[N];
bool inq[N];
int p[N];
int pid[N];
bool bfs() {
  memset(dist, 63, sizeof(dist));
  queue<int> q;
  q.push(st);
  dist[st] = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    inq[v] = 0;
    for (auto id : e[v]) {
      auto x = data[id];
      int u = x.v;
      if (dist[u] > dist[v] + x.c && x.f < x.mf) {
        dist[u] = dist[v] + x.c;
        p[u] = v;
        pid[u] = id;
        if (!inq[u]) {
          inq[u] = 1;
          q.push(u);
        }
      }
    }
  }
  if (dist[fn] < INF) {
    int v = fn;
    int flow = INF;
    for (; v != st; v = p[v]) {
      flow = min(flow, data[pid[v]].mf - data[pid[v]].f);
    }
    answer += flow * dist[fn];
    v = fn;
    for (; v != st; v = p[v]) {
      data[pid[v]].f += flow;
      data[pid[v] ^ 1].f -= flow;
    }
    return 1;
  }
  return 0;
}
void read() {
  scanf("%d%d", &n, &m);
  st = n;
  fn = n + 1;
  all = n + 2;
  addE(n - 1, 0, INF, 0);
  for (int i = 0; i < m; i++) {
    int v, u, c, f;
    scanf("%d%d%d%d", &v, &u, &c, &f);
    v--;
    u--;
    addE(st, u, f, 0);
    addE(v, fn, f, 0);
    if (f <= c) {
      addE(u, v, f, 1);
      addE(v, u, c - f, 1);
      addE(v, u, INF, 2);
    } else {
      answer += f - c;
      addE(u, v, f - c, 0);
      addE(u, v, c, 1);
      addE(v, u, INF, 2);
    }
  }
}
void solve() {
  while (bfs())
    ;
  cout << answer << endl;
}
void stress() {}
int main() {
  if (1) {
    int k = 1;
    for (int tt = 0; tt < k; tt++) {
      read();
      solve();
    }
  } else {
    stress();
  }
  return 0;
}
