#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) nat[i] *= -1;
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
//		printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 1, n){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		//F.insert(a, b + n, 1, c);
		//F2.insert(a, n + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
			c = d;
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
		F.insert(i, i + n, 0, inf / 10);
		F2.insert(i, i + n, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
//		printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		//F.insert(a, b + n, 1, c);
		//F2.insert(a, n + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
			c = d;
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 1000);
	F.insert(S, n, 0, inf / 1000);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 1000);
		F2.insert(i + n, i, 0, inf / 1000);
	}
	F2.insert(n + 1, T, 0, inf / 1000);
	F2.insert(n + n, T, 0, inf / 1000);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 1000);
		F2.insert(i + n, i, 0, inf / 1000);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 1000);
		F2.insert(a, b + n, 2, inf / 1000);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
//		printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 1, n){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		//F.insert(a, b + n, 1, c);
		//F2.insert(a, n + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
			c = d;
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i, i + n, 0, inf / 10);
		F2.insert(i, i + n, 0, inf / 10);
	}
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
	//	printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);
	FOE(i, 2, n - 1){
		F.insert(i + n, i, 0, inf / 10);
		F2.insert(i + n, i, 0, inf / 10);
	}

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		//F.insert(a, b + n, 1, c);
		//F2.insert(a, n + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iostream>
#include <set>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
#define FOR(i,s,e) for (int i=(s); i<(e); i++)
#define FOE(i,s,e) for (int i=(s); i<=(e); i++)
#define FOD(i,s,e) for (int i=(s); i>=(e); i--)
#define LL long long
#define eps 1e-6
#define pi acos(-1.0)
#define fail {printf("Impossible\n"); return 0;}
LL max(LL a,LL b){if (a>b){return a;} else {return b;}}
LL min(LL a,LL b){if (a<b){return a;} else {return b;}}

#define M 500000
#define inf (LL)1000000000000000

int n, m;

struct CMaxFlow{
    LL nxt[M], dir[M], h, n, que[M], rev[M], cost[M], f[M], dis[M], vis[M];

    void init(int cnt){
        FOE(i, 1, cnt) nxt[i] = -1;
        n = h = cnt;
    }

    //undirected: add twice
    //format: u->v, cost c, flow d

    void insert(int u, int v, LL c, LL w){
//		printf("build %d->%d, cost %lld, flow %lld\n", u, v, c, w);
        nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; cost[h] = c; f[h] = w;
        nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; cost[h] = -c; f[h] = 0;
    }
    LL finalflow, finalcost;
    LL d[M], sus[M], pre[M], id[M];

    bool ShortestPath(int S, int T){
        int head = 0, tail = 1;
        FOE(i, 0, n){
            dis[i] = inf;
            vis[i] = false;
        }
        dis[S] = 0;
        que[0] = S;
        while(tail != head){
            int u = que[head];
            vis[u] = true;
            for (int po = nxt[u]; po > 0; po = nxt[po]){
                if(f[po] && dis[dir[po]] > dis[u] + cost[po]){
                    dis[dir[po]] = dis[u] + cost[po];
                    pre[dir[po]] = u;
                    id[dir[po]] = po;
                    if(!vis[dir[po]]){
                        vis[dir[po]] = true;
                        que[tail++] = dir[po];
                        tail %= M;
                    }
                }}
            vis[u] = false;
            head = (head + 1) % M;
        }
        if(dis[T] == inf) return false;
        return true;
    }

    void fix(int S, int u){
        if (u == S) return;
        int neg = pre[u];
        sus[neg] = u;
        fix(S, neg);
    }

    LL argu(int u, int T, LL flow){
        if (u == T) return flow;
        int ee = id[sus[u]];
        LL ff = argu(sus[u], T, min(flow, f[ee]));
    //	printf("%d->%d via %d, flow %d\n", u, sus[u], ee, ff);
        f[ee] -= ff;
        f[rev[ee]] += ff;
        finalcost += cost[ee] * ff;
        return ff;
    }

    void CostedMaxFlow(int S, int T){
        finalflow = finalcost = 0;
        while (ShortestPath(S, T)){
            fix(S, T);
            finalflow += argu(S, T, inf);
        }
        return;
    }
}F, F2;

int S, T;
LL prepaid;

#define K 301
int nat[K];

int main(){
	scanf("%d%d", &n, &m);
	F.init(n * 2 + 2);
	F2.init(n * 2 + 2);
	S = n * 2 + 1; T = n * 2 + 2;
	F.insert(S, 1, 0, inf / 10);
	F.insert(S, n, 0, inf / 10);
	F2.insert(n + 1, T, 0, inf / 10);
	F2.insert(n + n, T, 0, inf / 10);

	FOE(i, 1, m){
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &d, &c);
		F.insert(b, a + n, 1, c);
		F2.insert(b, a + n, 1, c);
		if (d < c){
			prepaid += (LL)(c - d);
			F.insert(a, b + n, 0, c - d);
			F2.insert(a, b + n, 0, c - d);
		} else {
			F.insert(a, b + n, 1, d - c);
			F2.insert(a, b + n, 1, d - c);
		}
		F.insert(a, b + n, 2, inf / 10);
		F2.insert(a, b + n, 2, inf / 10);
		nat[a] += c; nat[b] -= c;
	}

	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, n) if (i != 1 && i != n) if (nat[i] > 0) F2.insert(S, i, 0, nat[i]);
	else F2.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	F2.CostedMaxFlow(S, T);
	if (F.finalflow > F2.finalflow) F2.finalcost = F.finalcost;
	if (F.finalflow < F2.finalflow) F.finalcost = F2.finalcost;
	cout << min(F2.finalcost, F.finalcost) + prepaid << endl;
	return 0;
}


#include<bits/stdc++.h>
#define o 1000005
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pa pair<int,int>
#define mpp make_pair
#define s1 first
#define s2 second
using namespace std;
inline int read()
{
	register int data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=(data<<1)+(data<<3)+(ch^48),ch=getchar();
	return data*w;
}
int n,m,cnt=1,h[o];
struct node
{
	int to,next,flew,v;
}w[o];
void add(int x,int y,int flew,int v)
{
	//cout<<"x="<<x<<' '<<y<<' '<<flew<<' '<<v<<'\n';
	w[++cnt]=(node){y,h[x],flew,v};h[x]=cnt;
	w[++cnt]=(node){x,h[y],0,-v};h[y]=cnt;
}
bool Vis[o];
int q[o],dis[o],in[o],S,T,tot,Ans;
bool spfa()
{
	int l=1,r=0;
	memset(dis,0x3f,sizeof(int)*(T+1));
	q[++r]=T;dis[T]=0;
	while(l<=r)
	{
		int x=q[l++];in[x]=0;
		for(int i=h[x];i;i=w[i].next)
		{
			int y=w[i].to;
			if(w[i^1].flew&&dis[y]>dis[x]+w[i^1].v)
			{
				dis[y]=dis[x]+w[i^1].v;
				if(!in[y])q[++r]=y,in[y]=1;
			}
		}
	}
	//FOR(i,1,T)cout<<dis[i]<<' ';
	return dis[S]<dis[0];
}
int dfs(int x,int mf)
{
	Vis[x]=1;
	if(x==T)return mf;
	int ret=0;
	for(int i=h[x];i;i=w[i].next)
	{
		int y=w[i].to;
		if((!Vis[y]||y==T)&&w[i].flew&&dis[y]+w[i].v==dis[x])
		{
			//cout<<x<<' '<<y<<'\n';
			int dlt=dfs(y,min(mf,w[i].flew));
			Ans+=dlt*w[i].v;
			w[i].flew-=dlt;w[i^1].flew+=dlt;ret+=dlt;mf-=dlt;
			if(!mf)return ret;
		}
	}
	return ret;
}
void ZKW()
{
	while(spfa())
	{
		Vis[T]=1;
		while(Vis[T]){memset(Vis,0,sizeof(int)*(T+1));dfs(S,0x3f3f3f3f);}
	}
}
int ot[o];
int main()
{
	n=read(),m=read();
	FOR(i,1,m)
	{
		int u=read(),v=read(),c=read(),f=read();
		ot[u]+=f;ot[v]-=f;
		if(f<=c)add(v,u,f,1),add(u,v,c-f,1),add(u,v,0x3f3f3f3f,2);
		else add(v,u,f-c,0),add(v,u,c,1),add(u,v,0x3f3f3f3f,2),Ans+=f-c;
	}
	//cout<<Ans<<'\n';
	add(n,1,0x3f3f3f3f,0);
	S=n+1,T=n+2;
	FOR(i,1,n)(ot[i]>=0?add(i,T,ot[i],0):add(S,i,-ot[i],0));
	ZKW();
	cout<<Ans<<'\n';
	return 0;
}


#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 1e3 + 50;
typedef long long ll;
int n, m; ll dg[N];
namespace LP{
	cs double eps = 1e-10, INF = 1e17;
	double a[N][N]; int n, m;
	int id[N];
	void pivot(int l, int e){
		double t=a[l][e]; a[l][e]=1;
		swap(id[l+n],id[e]);
		for(int i=0; i<=m; i++) a[l][i]/=t;
		for(int i=0; i<=n; i++) if(l!=i&&fabs(a[i][e])>0){
			t=a[i][e]; a[i][e]=0;
			for(int j=0; j<=m; j++) a[i][j]-=t*a[l][j];
		}
	}
	int simplex(){
		while(true){
			int l=0, e=0; double mn=INF;
			for(int i=1; i<=m; i++) if(a[0][i]>eps){ e=i; break; }
			if(!e) break;
			for(int i=1; i<=n; i++) if(a[i][e]>eps&&a[i][0]/a[i][e]<mn)
			mn=a[i][0]/a[i][e], l=i; if(!l) return -1; //unbounded
			pivot(l,e);
		} return (ll)-a[0][0];
	}
} using LP::a;
int main(){
	#ifdef FSYolanda
	freopen("1.in","r",stdin);
	#endif
	scanf("%d%d",&n,&m);
	for(int i=1,u,v,c,f; i<=m; i++){
		scanf("%d%d%d%d",&u,&v,&c,&f);
		a[0][i+m+n+n]=-f;
		a[0][i]=f-c; dg[u]+=f; dg[v]-=f;
		++LP::m; a[LP::m][0]=1; a[LP::m][i]=-1;
		if(v<n) a[LP::m][v+m]=1, a[LP::m][v+m+n]=-1;
		if(u>1) a[LP::m][u+m]=-1, a[LP::m][u+m+n]=1;

		++LP::m; a[LP::m][0]=1; a[LP::m][i]=1; a[LP::m][i+m+n+n]=-1;
		if(v<n) a[LP::m][v+m]=-1, a[LP::m][v+m+n]=1;
		if(u>1) a[LP::m][u+m]=1, a[LP::m][u+m+n]=-1;
	} for(int i=1; i<=m; i++) ++LP::m, a[LP::m][0]=1, a[LP::m][i]=1;
	for(int i=2; i<n; i++) a[0][i+m]=dg[i], a[0][i+m+n]=-dg[i];
	LP::n = m+m+n+n; swap(LP::n, LP::m);
	cout<<LP::simplex(); return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 110,inf = 0x7ffffff;
int head[maxn],to[5000010],nextt[5000010],w[5000010],cost[5000010],tot = 1,n,m,S,T,deg[maxn],ans;
int d[maxn],vis[maxn],vis2[maxn];

void add(int x,int y,int z,int p)
{
    cost[++tot] = p;
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot;

    cost[++tot] = -p;
    w[tot] = 0;
    to[tot] = x;
    nextt[tot] = head[y];
    head[y] = tot;
}

bool spfa()
{
    for (int i = 1; i <= T; i++)
        d[i] = inf,vis[i] = vis2[i] = 0;
    queue <int> q;
    q.push(S);
    d[S] = 0;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (d[v] > d[u] + cost[i] && w[i])
            {
                d[v] = d[u] + cost[i];
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return d[T] < inf;
}

int dfs(int u,int f)
{
    if (u == T)
    {
        ans += f * d[u];
        return f;
    }
    vis2[u] = 1;
    int res = 0;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (d[v] == d[u] + cost[i] && w[i] && !vis2[v])
        {
            int temp = dfs(v,min(f - res,w[i]));
            w[i] -= temp;
            w[i ^ 1] += temp;
            res += temp;
            if (res == f)
                return res;
        }
    }
    return res;
}

void dinic()
{
    while (spfa())
        dfs(S,inf);
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i = 1; i <= m; i++)
    {
        int u,v,c,f;
        scanf("%d%d%d%d",&u,&v,&c,&f);
        if (f <= c)
        {
            add(v,u,f,1);
            add(u,v,c - f,1);
            add(u,v,inf,2);
            deg[u] += f;
            deg[v] -= f;
        }
        else
        {
            ans += f - c;
            add(v,u,c,1);
            add(u,v,f - c,0);
            add(u,v,inf,2);
            deg[u] += c;
            deg[v] -= c;
        }
    }
    S = n + 1,T = S + 1;
    for (int i = 1; i <= n; i++)
    {
        if (deg[i] > 0)
            add(i,T,deg[i],0);
        else
            add(S,i,-deg[i],0);
    }
    add(n,1,inf,0);
    dinic();
    printf("%d\n",ans);

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=105;
const int inf=1<<30;
struct edge{int to,nxt,w,cost;}a[N<<5];
int n,m,S,T,d[N],hd[N],cnt=1,vis[N],pe[N],dis[N],ans;
queue<int>Q;
void link(int u,int v,int w,int cost){
	a[++cnt]=(edge){v,hd[u],w,cost};hd[u]=cnt;
	a[++cnt]=(edge){u,hd[v],0,-cost};hd[v]=cnt;
}
bool spfa(){
	memset(dis,63,sizeof(dis));
	dis[S]=0;Q.push(S);
	while(!Q.empty()){
		int u=Q.front();Q.pop();
		for(int i=hd[u];i;i=a[i].nxt){
			int v=a[i].to;
			if(a[i].w&&dis[v]>dis[u]+a[i].cost){
				dis[v]=dis[u]+a[i].cost;pe[v]=i;
				if(!vis[v])vis[v]=1,Q.push(v);
			}
		}
		vis[u]=0;
	}
	if(dis[T]==dis[0])return false;
	int sum=1<<30;
	for(int i=T;i!=S;i=a[pe[i]^1].to)sum=min(sum,a[pe[i]].w);
	ans+=dis[T]*sum;
	for(int i=T;i!=S;i=a[pe[i]^1].to)a[pe[i]].w-=sum,a[pe[i]^1].w+=sum;
	return true;
}
int main(){
	scanf("%d%d",&n,&m);S=n+1;T=n+2;
	for(int i=1,u,v,c,f;i<=m;++i){
		scanf("%d%d%d%d",&u,&v,&c,&f);
		d[u]-=f;d[v]+=f;
		if(f<=c)link(u,v,c-f,1),link(u,v,inf,2),link(v,u,f,1);
		else ans+=f-c,link(u,v,inf,2),link(v,u,f-c,0),link(v,u,c,1);
	}
	link(n,1,inf,0);
	for(int i=1;i<=n;++i)
		if(d[i]>0)link(S,i,d[i],0);
		else link(i,T,-d[i],0);
	while(spfa());
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int read();
int n, m, s, t, res;
int hd[1003], nx[10004], to[10004], c[10004], w[10004], cnt;
int deg[1003];
void add(int f, int t, int cap, int cost) {
    nx[++cnt] = hd[f], hd[f] = cnt, to[cnt] = t, c[cnt] = cap, w[cnt] = cost;
}

void fadd(int u, int v, int cap, int cost) {
    add(u, v, cap, cost), add(v, u, 0, -cost);
}

queue<int> q;
int dis[1003], inq[1003], pv[1003], pe[1003];
void SPFA() {
    for (int i = 1; i <= t; ++i) dis[i] = 1000000009;
    q.push(s), inq[s] = 1, dis[s] = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop(), inq[u] = 0;
        for (int i = hd[u], v; i; i = nx[i]) {
            if (c[i] < 1 || dis[v = to[i]] <= dis[u] + w[i]) continue;
            dis[v] = dis[u] + w[i], pv[v] = u, pe[v] = i;
            if (!inq[v]) q.push(v), inq[v] = 1;
        }
    }
}

void mcf() {
    while (1) {
        SPFA();
        if (dis[t] >= 1000000000) break;
        int d = 10000000;
        for (int u = t; u != s; u = pv[u]) d = min(d, c[pe[u]]);
        res += d * dis[t];
        for (int u = t; u != s; u = pv[u]) c[pe[u]] -= d, c[pe[u] ^ 1] += d;
    }
}

int main() {
    n = read(), m = read(), cnt = 1, s = n + 1, t = n + 2;
    for (int i = 1, u, v, c, f; i <= m; ++i) {
        u = read(), v = read(), c = read(), f = read();
        (f > c) ? (res += f - c, fadd(v, u, f - c, 0), fadd(v, u, c, 1))
                : (fadd(v, u, f, 1), fadd(u, v, c - f, 1));
        fadd(u, v, 10000000, 2), deg[u] -= f, deg[v] += f;
    }
    fadd(n, 1, 1000000000, 0);
    for (int i = 1; i <= n; ++i)
        deg[i] > 0 ? fadd(s, i, deg[i], 0) : fadd(i, t, -deg[i], 0);
    mcf(), printf("%d", res);
    return 0;
}
const int _SIZE = 1 << 22;
char ibuf[_SIZE], *iS = ibuf, *iT = ibuf;
#define gc                                                         \
    (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), \
     (iS == iT ? EOF : *iS++) : *iS++)
int read() {
    int x = 0, f = 1;
    char c = gc;
    while (!isdigit(c)) f = (c == '-') ? -1 : f, c = gc;
    while (isdigit(c)) x = x * 10 + c - '0', c = gc;
    return x * f;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
/*
x->y

x in-out
*/
#ifdef emm
#define gc getchar
#endif
#ifndef emm
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
#endif
inline ll read(){
    register ll x=0,f=1;char ch=gc();
    while (!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
    while (isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=gc();}
    return (f==1)?x:-x;
}
struct edge{
	int to,nxt,f,c;
}g[10101];
int tot,head[10101];
inline void _made(int from,int to,int f,int c){
	g[++tot].to=to;g[tot].nxt=head[from];head[from]=tot;
	g[tot].f=f;g[tot].c=c;
}
inline void made(int from,int to,int f,int c){
	_made(from,to,f,c);_made(to,from,0,-c);
}
int n,m,s,t,dis[10101],flow[10101],pre[10101],las[10101];
bool vis[10101];
queue<int> dl;
inline bool spfa(int s,int t){
	while (!dl.empty()) dl.pop();
	for (int i=0;i<=n+1;i++) flow[i]=0x3f3f3f3f,dis[i]=0x3f3f3f3f,pre[i]=-1;
	for (int i=0;i<=n+1;i++) las[i]=-1;
	dl.push(s);dis[s]=0;
	while (!dl.empty()){
		int u=dl.front();dl.pop();vis[u]=0;
		for (int i=head[u];~i;i=g[i].nxt){
			int v=g[i].to;
			if (g[i].f>0&&g[i].c+dis[u]<dis[v]){
				dis[v]=dis[u]+g[i].c;
				flow[v]=min(flow[u],g[i].f);
				pre[v]=u,las[v]=i;
				if (!vis[v]) vis[v]=1,dl.push(v);
			}
		}
	}
	return (~pre[t]);
}
int maxflow,mincost;
inline void MCMF(){
    while (spfa(s,t)){
        int u=t;
        maxflow+=flow[t];
        mincost+=flow[t]*dis[t];
        while (u!=s){
            g[las[u]].f-=flow[t];
            g[las[u]^1].f+=flow[t];
            u=pre[u];
        }
    }
}
int e[1010][4],indo[1010];
signed main(){
	tot=-1;memset(head,-1,sizeof head);
	n=read(),m=read();
	s=0,t=n+1;
	for (int i=1;i<=m;i++){
		e[i][0]=read(),e[i][1]=read(),e[i][2]=read(),e[i][3]=read();
		indo[e[i][1]]+=e[i][3],indo[e[i][0]]-=e[i][3];
	}
	for (int i=1;i<=n;i++){
		if (indo[i]>0) made(s,i,indo[i],0);
		else if (indo[i]<0) made(i,t,-indo[i],0);
	}
	ll _=0;
	for (int i=1;i<=m;i++){
		int u=e[i][0],v=e[i][1],nc=e[i][2],nf=e[i][3];
		if (nf==nc){
			made(v,u,nc,1);
			made(u,v,0x3f3f3f3f,2);
		}else if (nf>nc){
			_+=nf-nc;
			made(v,u,nf-nc,0);
			made(v,u,nc,1);
			made(u,v,0x3f3f3f3f,2);
		}else{
			made(v,u,nf,1);
			made(u,v,nc-nf,2);
			made(u,v,0x3f3f3f3f,2);
		}
	}
	made(n,1,0x3f3f3f3f,0);
	MCMF();
	cout<<_+mincost;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <functional>
#include <ctime>
#include <limits>
#include <tuple>
#include <complex>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = acos(-1.0l);

template<typename T>
T abs(T x) {
    return x > 0 ? x : -x;
}

template<typename T>
T sqr(T x) {
    return x * x;
}

template<typename T>
bool chmin(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool chmax(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

template<typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
    s << "(" << x.fst << ", " << x.snd << ")";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const vector<U> &x) {
    s << "[";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "]";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const set<U> &x) {
    s << "{";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "}";
    return s;
}

template<int sz>
ostream operator<<(ostream &s, const bitset<sz> &x) {
    for (int i = 0; i < sz; i++) {
        s << x[i];
    }
    return s;
}

//-----------------------------------------------------------------------------

const int maxn = 405;
const int maxe = 1000000;
const ll inf = 1e18;
ll f[maxe], c[maxe], p[maxe];
ll ph[maxn];

ll dist[maxn];
int s, t;

int ecnt;
vector<pair<int, int>> g[maxn];

int prV[maxn], prE[maxn];

int newS = maxn - 3, newT = maxn - 4;

void add(int u, int v, int cc, int pp) {
    g[u].pb(mp(v, ecnt));
    c[ecnt] = cc;
    g[v].pb(mp(u, ecnt + 1));
    p[ecnt] = pp;
    p[ecnt + 1] = -pp;
    ecnt += 2;
}

void ford() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;
    for (int i = 0; i < maxn; i++) {
        for (int j = 0; j < maxn; j++) {
            for (auto to : g[i]) {
                if (dist[to.fst] > dist[i] + p[to.snd] && f[to.snd] < c[to.snd]) {
                    dist[to.fst] = dist[i] + p[to.snd];
                }
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] = dist[i];
    }
}

void djkstra() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;

    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
    q.push(mp(0, newS));

    while (!q.empty()) {
        int v = q.top().snd;
        ll cd = q.top().fst;
        q.pop();

        if (dist[v] != cd) {
            continue;
        }

        for (auto to : g[v]) {
            if (f[to.snd] == c[to.snd]) {
                continue;
            }

            ll nd = dist[v] + ph[v] - ph[to.fst] + p[to.snd];
            if (nd < dist[to.fst]) {
                dist[to.fst] = nd;
                prV[to.fst] = v;
                prE[to.fst] = to.snd;
                q.push(mp(nd, to.fst));
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] += (dist[i] == inf ? 0 : dist[i]);
    }
}

ll flow() {
    ford();
    while (true) {
        djkstra();
        if (dist[newT] == inf) {
            break;
        }

        int v = newT;
        ll rr = inf;
        while (v != newS) {
            rr = min(rr, c[prE[v]] - f[prE[v]]);
            v = prV[v];
        }
        //cout << " rr" << rr << endl;

        v = newT;
        while (v != newS) {
            f[prE[v]] += rr;
            //cout << p[prE[v]]  << " " << p[prE[v] ^ 1]<< endl;
            f[prE[v] ^ 1] -= rr;
            v = prV[v];
        }
    }

    ll cost = 0;
    for (int i = 0; i < ecnt; i += 2) {
        cost += f[i] * p[i];
        //cout << f[i] << " " << p[i] << endl;
    }

    return cost;
}

struct edge {
    int u, v, c, f;
};

ll bal[maxn];

void add2(int u, int v, int p, int l, int r) {
    add(newS, v, l, 0);
    add(u, newT, l, 0);
    add(u, v, r - l, p);
}

int dsu[maxn];
int find(int v) {
    if (dsu[v] == v) {
        return v;
    }
    return dsu[v] = find(dsu[v]);
}

int main() {
    srand(time(NULL));

    retry:
#ifdef LOCAL
   // gen();

    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;

    vector<edge> e;

    iota(dsu, dsu + maxn, 0);

    for (int i = 0; i < m; i++) {
        int u, v, c, f;
        cin >> u >> v >> c >> f;
        dsu[find(u)] = find(v);
        e.pb({u, v, c, f});
        bal[u] -= f;
        bal[v] += f;
    }

    s = maxn - 1, t = maxn - 2;

    for (int i = 2; i < n; i++) {

        if (bal[i] < 0) {
            add2(i, t, 0, -bal[i], -bal[i]);
        } else if (bal[i] > 0) {
            add2(s, i, 0, bal[i], bal[i]);
        } else if (bal[i] == 0) {
            //add2(s, i, 0, 0, 1e9);
            //add2(i, t, 0, 0, 1e9);
        }
    }

    add2(s, 1, 0, 0, 1e9);
    add2(1, t, 0, 0, 1e9);
    add2(s, n, 0, 0, 1e9);
    add2(n, t, 0, 0, 1e9);

    add(t, s, 1e9, 0);

    ll ans = 0;

    for (auto ee : e) {
        if (ee.f <= ee.c) {
            add2(ee.v, ee.u, 1, 0, ee.f);
            add2(ee.u, ee.v, 1, 0, ee.c - ee.f);
            add2(ee.u, ee.v, 2, 0, 1e9);
        } else {
            ans += ee.f - ee.c;
            add2(ee.v, ee.u, 0, 0, ee.f - ee.c);
            add2(ee.v, ee.u, 1, 0, ee.c);
            add2(ee.u, ee.v, 2, 0, 1e9);
        }
    }

    ans += flow();

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <functional>
#include <ctime>
#include <limits>
#include <tuple>
#include <complex>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = acos(-1.0l);

template<typename T>
T abs(T x) {
    return x > 0 ? x : -x;
}

template<typename T>
T sqr(T x) {
    return x * x;
}

template<typename T>
bool chmin(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool chmax(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

template<typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
    s << "(" << x.fst << ", " << x.snd << ")";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const vector<U> &x) {
    s << "[";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "]";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const set<U> &x) {
    s << "{";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "}";
    return s;
}

template<int sz>
ostream operator<<(ostream &s, const bitset<sz> &x) {
    for (int i = 0; i < sz; i++) {
        s << x[i];
    }
    return s;
}

//-----------------------------------------------------------------------------

const int maxn = 405;
const int maxe = 1000000;
const ll inf = 1e18;
ll f[maxe], c[maxe], p[maxe];
ll ph[maxn];

ll dist[maxn];
int s, t;

int ecnt;
vector<pair<int, int>> g[maxn];

int prV[maxn], prE[maxn];

int newS = maxn - 3, newT = maxn - 4;

void add(int u, int v, int cc, int pp) {
    g[u].pb(mp(v, ecnt));
    c[ecnt] = cc;
    g[v].pb(mp(u, ecnt + 1));
    p[ecnt] = pp;
    p[ecnt + 1] = -pp;
    ecnt += 2;
}

void ford() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;
    for (int i = 0; i < maxn; i++) {
        for (int j = 0; j < maxn; j++) {
            for (auto to : g[i]) {
                if (dist[to.fst] > dist[i] + p[to.snd] && f[to.snd] < c[to.snd]) {
                    dist[to.fst] = dist[i] + p[to.snd];
                }
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] = dist[i];
    }
}

void djkstra() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;

    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
    q.push(mp(0, newS));

    while (!q.empty()) {
        int v = q.top().snd;
        ll cd = q.top().fst;
        q.pop();

        if (dist[v] != cd) {
            continue;
        }

        for (auto to : g[v]) {
            if (f[to.snd] == c[to.snd]) {
                continue;
            }

            ll nd = dist[v] + ph[v] - ph[to.fst] + p[to.snd];
            if (nd < dist[to.fst]) {
                dist[to.fst] = nd;
                prV[to.fst] = v;
                prE[to.fst] = to.snd;
                q.push(mp(nd, to.fst));
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] += (dist[i] == inf ? 0 : dist[i]);
    }
}

ll flow() {
    ford();
    while (true) {
        djkstra();
        if (dist[newT] == inf) {
            break;
        }

        int v = newT;
        ll rr = inf;
        while (v != newS) {
            rr = min(rr, c[prE[v]] - f[prE[v]]);
            v = prV[v];
        }
        //cout << " rr" << rr << endl;

        v = newT;
        while (v != newS) {
            f[prE[v]] += rr;
            //cout << p[prE[v]]  << " " << p[prE[v] ^ 1]<< endl;
            f[prE[v] ^ 1] -= rr;
            v = prV[v];
        }
    }

    ll cost = 0;
    for (int i = 0; i < ecnt; i += 2) {
        cost += f[i] * p[i];
        //cout << f[i] << " " << p[i] << endl;
    }

    return cost;
}

struct edge {
    int u, v, c, f;
};

ll bal[maxn];

void add2(int u, int v, int p, int l, int r) {
    add(newS, v, l, 0);
    add(u, newT, l, 0);
    add(u, v, r - l, p);
}

int dsu[maxn];
int find(int v) {
    if (dsu[v] == v) {
        return v;
    }
    return dsu[v] = find(dsu[v]);
}

int main() {
    srand(time(NULL));

    retry:
#ifdef LOCAL
   // gen();

    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;

    vector<edge> e;

    iota(dsu, dsu + maxn, 0);

    for (int i = 0; i < m; i++) {
        int u, v, c, f;
        cin >> u >> v >> c >> f;
        dsu[find(u)] = find(v);
        e.pb({u, v, c, f});
        bal[u] -= f;
        bal[v] += f;
    }

    s = maxn - 1, t = maxn - 2;

    for (int i = 2; i < n; i++) {

        if (bal[i] < 0) {
            add2(i, t, 0, -bal[i], -bal[i]);
        } else if (bal[i] > 0) {
            add2(s, i, 0, bal[i], bal[i]);
        } else if (bal[i] == 0) {
            add2(s, i, 0, 0, 1e9);
            add2(i, t, 0, 0, 1e9);
        }
    }

    add2(s, 1, 0, 0, 1e9);
    add2(1, t, 0, 0, 1e9);
    add2(s, n, 0, 0, 1e9);
    add2(n, t, 0, 0, 1e9);

    add(t, s, 1e9, 0);

    ll ans = 0;

    for (auto ee : e) {
        if (ee.f <= ee.c) {
            add2(ee.v, ee.u, 1, 0, ee.f);
            add2(ee.u, ee.v, 1, 0, ee.c - ee.f);
            add2(ee.u, ee.v, 2, 0, 1e9);
        } else {
            ans += ee.f - ee.c;
            add2(ee.v, ee.u, 0, 0, ee.f - ee.c);
            add2(ee.v, ee.u, 1, 0, ee.c);
            add2(ee.u, ee.v, 2, 0, 1e9);
        }
    }

    ans += flow();

    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <ctype.h>
#include <cassert>
#include <stack>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <functional>
#include <ctime>
#include <limits>
#include <tuple>
#include <complex>

using namespace std;

#define snd second
#define fst first
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define left _left
#define right _right

const ld pi = acos(-1.0l);

template<typename T>
T abs(T x) {
    return x > 0 ? x : -x;
}

template<typename T>
T sqr(T x) {
    return x * x;
}

template<typename T>
bool chmin(T &x, T y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

template<typename T>
bool chmax(T &x, T y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

template<typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
    s << "(" << x.fst << ", " << x.snd << ")";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const vector<U> &x) {
    s << "[";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "]";
    return s;
}

template<typename U>
ostream &operator<<(ostream &s, const set<U> &x) {
    s << "{";
    bool was = false;
    for (auto it : x) {
        if (was) {
            s << ", ";
        }
        was = true;
        s << it;
    }
    s << "}";
    return s;
}

template<int sz>
ostream operator<<(ostream &s, const bitset<sz> &x) {
    for (int i = 0; i < sz; i++) {
        s << x[i];
    }
    return s;
}

//-----------------------------------------------------------------------------

const int maxn = 405;
const int maxe = 1000000;
const ll inf = 1e18;
ll f[maxe], c[maxe], p[maxe];
ll ph[maxn];

ll dist[maxn];
int s, t;

int ecnt;
vector<pair<int, int>> g[maxn];

int prV[maxn], prE[maxn];

int newS = maxn - 3, newT = maxn - 4;

void add(int u, int v, int cc, int pp) {
    g[u].pb(mp(v, ecnt));
    c[ecnt] = cc;
    g[v].pb(mp(u, ecnt + 1));
    p[ecnt] = pp;
    p[ecnt + 1] = -pp;
    ecnt += 2;
}

void ford() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;
    for (int i = 0; i < maxn; i++) {
        for (int j = 0; j < maxn; j++) {
            for (auto to : g[i]) {
                if (dist[to.fst] > dist[i] + p[to.snd] && f[to.snd] < c[to.snd]) {
                    dist[to.fst] = dist[i] + p[to.snd];
                }
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] = dist[i];
    }
}

void djkstra() {
    fill(dist, dist + maxn, inf);
    dist[newS] = 0;

    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
    q.push(mp(0, newS));

    while (!q.empty()) {
        int v = q.top().snd;
        ll cd = q.top().fst;
        q.pop();

        if (dist[v] != cd) {
            continue;
        }

        for (auto to : g[v]) {
            if (f[to.snd] == c[to.snd]) {
                continue;
            }

            ll nd = dist[v] + ph[v] - ph[to.fst] + p[to.snd];
            if (nd < dist[to.fst]) {
                dist[to.fst] = nd;
                prV[to.fst] = v;
                prE[to.fst] = to.snd;
                q.push(mp(nd, to.fst));
            }
        }
    }

    for (int i = 0; i < maxn; i++) {
        ph[i] += (dist[i] == inf ? 0 : dist[i]);
    }
}

ll flow() {
    ford();
    while (true) {
        djkstra();
        if (dist[newT] == inf) {
            break;
        }

        int v = newT;
        ll rr = inf;
        while (v != newS) {
            rr = min(rr, c[prE[v]] - f[prE[v]]);
            v = prV[v];
        }

        v = newT;
        while (v != newS) {
            f[prE[v]] += rr;
            f[prE[v] ^ 1] -= rr;
            v = prV[v];
        }
    }

    ll cost = 0;
    for (int i = 0; i < ecnt; i += 2) {
        cost += f[i] * p[i];
    }

    return cost;
}

struct edge {
    int u, v, c, f;
};

ll bal[maxn];

void add2(int u, int v, int p, int l, int r) {
    add(newS, v, l, 0);
    add(u, newT, l, 0);
    add(u, v, r - l, p);
}

int main() {
    srand(time(NULL));

    retry:
#ifdef LOCAL
   // gen();

    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
#endif

    int n, m;
    cin >> n >> m;

    vector<edge> e;

    for (int i = 0; i < m; i++) {
        int u, v, c, f;
        cin >> u >> v >> c >> f;
        e.pb({u, v, c, f});
        bal[u] -= f;
        bal[v] += f;
    }

    s = maxn - 1, t = maxn - 2;

    for (int i = 2; i < n; i++) {
        if (bal[i] < 0) {
            add2(i, t, 0, -bal[i], -bal[i]);
        } else if (bal[i] > 0) {
            add2(s, i, 0, bal[i], bal[i]);
        }
    }

    add2(s, 1, 0, 0, 1e9);
    add2(1, t, 0, 0, 1e9);
    add2(s, n, 0, 0, 1e9);
    add2(n, t, 0, 0, 1e9);

    add(t, s, 1e9, 0);

    ll ans = 0;

    for (auto ee : e) {
        if (ee.f <= ee.c) {
            add2(ee.v, ee.u, 1, 0, ee.f);
            add2(ee.u, ee.v, 1, 0, ee.c - ee.f);
            add2(ee.u, ee.v, 2, 0, 1e9);
        } else {
            ans += ee.f - ee.c;
            add2(ee.v, ee.u, 1, 0, ee.f - ee.c);
            add2(ee.v, ee.u, 2, 0, ee.c);
            add2(ee.u, ee.v, 2, 0, 1e9);
        }
    }

    ans += flow();

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 105;
const int M = 1005;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0X3f3f3f3f3f3f3f3f;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

template <typename T> inline void cmin(T &x, T y) {
	if (x > y) x = y;
}

template <typename T> inline void cmax(T &x, T y) {
	if (x < y) x = y;
}

int deg[N], S, T, inq[N], pre[N];
LL dis[N];
queue<int> q;

struct Edge {
	int v, w, nxt;
	LL c;
} e[M << 1];
int first[N], eCnt;

inline void AddEdge(int u, int v, int w, LL c) {
	e[++eCnt].v = v;
	e[eCnt].w = w;
	e[eCnt].c = c;
	e[eCnt].nxt = first[u];
	first[u] = eCnt;
}

inline void Add(int u, int v, int w, LL c) {
	AddEdge(u, v, w, c);
	AddEdge(v, u, 0, -c);
}

bool SPFA() {
	for (int i = 1; i <= T; ++i) {
		dis[i] = LLINF;
	}
	dis[S] = 0;
	q.push(S);
	while (!q.empty()) {
		int u = q.front();
		q.pop(); inq[u] = 0;
		for (int i = first[u]; i; i = e[i].nxt) {
			int v = e[i].v;
			if (e[i].w && dis[v] > dis[u] + e[i].c) {
				dis[v] = dis[u] + e[i].c;
				pre[v] = i;
				if (!inq[v]) {
					q.push(v);
					inq[v] = 1;
				}
			}
		}
	}
	return (dis[T] < LLINF);
}

LL Augment() {
	int flow = INF;
	for (int i = pre[T]; i; i = pre[e[i ^ 1].v]) {
		cmin(flow, e[i].w);
	}
	for (int i = pre[T]; i; i = pre[e[i ^ 1].v]) {
		e[i].w -= flow;
		e[i ^ 1].w += flow;
	}
	return dis[T] * flow;
}

void solve() {
	LL ans = 0;
	int n, m;
	read(n); read(m);
	eCnt = 1;
	S = n + 1, T = S + 1;
	for (int i = 1; i <= m; ++i) {
		int u, v, c, f;
		read(u); read(v); read(c); read(f);
		if (f <= c) {
			Add(u, v, c - f, 1);
			Add(u, v, INF, 2);
			Add(v, u, f, 1);
			deg[u] -= f; deg[v] += f;
		} else {
			ans += f - c;
			Add(u, v, INF, 2);
			Add(v, u, f - c, 0);
			Add(v, u, c, 1);
			deg[u] -= f; deg[v] += f;
		}
	}
	Add(n, 1, INF, 0);
	for (int i = 1; i <= n; ++i) {
		if (deg[i] > 0) {
			Add(S, i, deg[i], 0);
		} else if (deg[i] < 0) {
			Add(i, T, -deg[i], 0);
		}
	}
	while (SPFA()) {
		ans += Augment();
	}
	printf("%lld\n", ans);
}

int main() {
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 105;
const int M = 1005;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0X3f3f3f3f3f3f3f3f;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

template <typename T> inline void cmin(T &x, T y) {
	if (x > y) x = y;
}

template <typename T> inline void cmax(T &x, T y) {
	if (x < y) x = y;
}

int deg[N], S, T, inq[N], pre[N];
LL dis[N];
queue<int> q;

struct Edge {
	int v, w, nxt;
	LL c;
} e[M << 1];
int first[N], eCnt;

inline void AddEdge(int u, int v, int w, LL c) {
	e[++eCnt].v = v;
	e[eCnt].w = w;
	e[eCnt].c = c;
	e[eCnt].nxt = first[u];
	first[u] = eCnt;
}

inline void Add(int u, int v, int w, LL c) {
	AddEdge(u, v, w, c);
	AddEdge(v, u, 0, -c);
}

bool SPFA() {
	for (int i = 1; i <= T; ++i) {
		dis[i] = LLINF;
	}
	dis[S] = 0;
	q.push(S);
	while (!q.empty()) {
		int u = q.front();
		q.pop(); inq[u] = 0;
		for (int i = first[u]; i; i = e[i].nxt) {
			int v = e[i].v;
			if (e[i].w && dis[v] > dis[u] + e[i].c) {
				dis[v] = dis[u] + e[i].c;
				pre[v] = i;
				if (!inq[v]) {
					q.push(v);
					inq[v] = 1;
				}
			}
		}
	}
	return (dis[T] < LLINF);
}

LL Augment() {
	int flow = INF;
	for (int i = pre[T]; i; i = pre[e[i ^ 1].v]) {
		cmin(flow, e[i].w);
	}
	for (int i = pre[T]; i; i = pre[e[i ^ 1].v]) {
		e[i].w -= flow;
		e[i ^ 1].w += flow;
	}
	return dis[T] * flow;
}

void solve() {
	LL ans = 0;
	int n, m;
	read(n); read(m);
	eCnt = 1;
	S = n + 1, T = S + 1;
	for (int i = 1; i <= m; ++i) {
		int u, v, c, f;
		read(u); read(v); read(c); read(f);
		if (f <= c) {
			Add(u, v, c - f, 1);
			Add(u, v, INF, 2);
			Add(v, u, f, 1);
			deg[u] -= f; deg[v] += f;
		} else {
			ans += f - c;
			Add(u, v, f - c, 0);
			Add(u, v, INF, 2);
			Add(v, u, c, 1);
			deg[u] -= c; deg[v] += c;
		}
	}
	Add(n, 1, INF, 0);
	for (int i = 1; i <= n; ++i) {
		if (deg[i] > 0) {
			Add(S, i, deg[i], 0);
		} else if (deg[i] < 0) {
			Add(i, T, -deg[i], 0);
		}
	}
	while (SPFA()) {
		ans += Augment();
	}
	printf("%lld\n", ans);
}

int main() {
	solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x)*(x))
#define sz(v) int(v.size())
#define par make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
template<class X, class Y> void amax(X& x, const Y& y){if(x < y) x = y;}
template<class X, class Y> void amin(X& x, const Y& y){if(x > y) x = y;}
typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; typedef long double ld;
const int INF = 1e9 + 10; const ll INFL = 1e18 + 10;
const int MAX = 110;
struct edge{
    int u, v, f, c;
};

struct mincost{
    int n, s, t;
    vector<edge> ed;
    vector<vi> es;
    vector<bool> mark;
    vi trace, dd;

    mincost(int n, int s, int t): n(n), s(s), t(t){
        es.resize(n);
        trace.resize(n);
        mark.resize(n);
        dd.resize(n);
    }

    void add(int u, int v, int f, int c){
        edge e;
        e.u = u;
        e.v = v;
        e.f = f;
        e.c = c;
        ed.pb(e);
        es[e.u].pb(sz(ed) - 1);
        swap(e.u, e.v);
        e.f = 0;
        e.c = -e.c;
        ed.pb(e);
        es[e.u].pb(sz(ed) - 1);
    }

    bool spfa(){
        fill(dd.begin(), dd.end(), INF);
        fill(mark.begin(), mark.end(), false);
        queue<int> qe;
        qe.push(s);
        dd[s] = 0; mark[s] = true;
        while(!qe.empty()){
            int u = qe.front(); qe.pop();
            mark[u] = false;
            for(auto e : es[u])
            if(ed[e].f && dd[ed[e].v] > dd[u] + ed[e].c){
                dd[ed[e].v] = dd[u] + ed[e].c;
                trace[ed[e].v] = e;
                if(!mark[ed[e].v]){
                    mark[ed[e].v] = true;
                    qe.push(ed[e].v);
                }
            }
        }
        return dd[t] != INF;
    }

    ll flow(){
        ll res = 0;
        while(spfa()){
            int x = t, inc = INF;
            while(x != s){
                amin(inc, ed[trace[x]].f);
                x = ed[trace[x]].u;
            }
            x = t;
            while(x != s){
                ed[trace[x]].f -= inc;
                ed[trace[x] ^ 1].f += inc;
                x = ed[trace[x]].u;
            }
            res += (ll)dd[t]*inc;
        }
        return res;
    }
};

int n, m;
int b[MAX];
ll res;

void process(){
    cin >> n >> m;
    mincost mc(n + 2, 0, n + 1);
    for(int i = 1; i <= m; i++){
        int u, v, c, f;
        cin >> u >> v >> c >> f;
        b[u] += f;
        b[v] -= f;
        mc.add(u, v, INF, 2);
        if(c < f){
            res += f - c;
            mc.add(v, u, f - c, 0);
            mc.add(v, u, c, 1);
        }else{
            mc.add(u, v, c - f, 1);
            mc.add(v, u, f, 1);
        }
    }

    for(int i = 1; i <= n; i++){
        if(b[i] < 0){
            mc.add(0, i, -b[i], 0);
        }else if(b[i] > 0){
            mc.add(i, n + 1, b[i], 0);
        }
    }
    mc.add(n, 1, INF, 0);
    cout << res + mc.flow();
}

int main(){
    ios_base::sync_with_stdio(false);
    //~ cin.tie(nullptr);
    //~ freopen("d.in", "r", stdin);
    process();
}


#include <bits/stdc++.h>
using namespace std;
#define sqr(x) ((x)*(x))
#define sz(v) int(v.size())
#define par make_pair
#define pb push_back
#define fi first
#define se second
#define endl '\n'
template<class X, class Y> void amax(X& x, const Y& y){if(x < y) x = y;}
template<class X, class Y> void amin(X& x, const Y& y){if(x > y) x = y;}
typedef vector<int> vi; typedef pair<int, int> ii; typedef long long ll; typedef long double ld;
const int INF = 1e9 + 10; const ll INFL = 1e18 + 10;
const int MAX = 110;
struct edge{
    int u, v, f, c;
};

struct mincost{
    int n, s, t;
    vector<edge> ed;
    vector<vi> es;
    vector<bool> mark;
    vi trace, dd;

    mincost(int n, int s, int t): n(n), s(s), t(t){
        es.resize(n);
        trace.resize(n);
        mark.resize(n);
        dd.resize(n);
    }

    void add(int u, int v, int f, int c){
        edge e;
        e.u = u;
        e.v = v;
        e.f = f;
        e.c = c;
        ed.pb(e);
        es[e.u].pb(sz(ed) - 1);
        swap(e.u, e.v);
        e.f = 0;
        e.c = -e.c;
        ed.pb(e);
        es[e.u].pb(sz(ed) - 1);
    }

    bool spfa(){
        fill(dd.begin(), dd.end(), INF);
        fill(mark.begin(), mark.end(), false);
        queue<int> qe;
        qe.push(s);
        dd[s] = 0; mark[s] = true;
        while(!qe.empty()){
            int u = qe.front(); qe.pop();
            for(auto e : es[u])
            if(ed[e].f && dd[ed[e].v] > dd[u] + ed[e].c){
                dd[ed[e].v] = dd[u] + ed[e].c;
                trace[ed[e].v] = e;
                if(!mark[ed[e].v]){
                    mark[ed[e].v] = true;
                    qe.push(ed[e].v);
                }
            }
        }
        return dd[t] != INF;
    }

    int flow(){
        ll res = 0;
        while(spfa()){
            int x = t, inc = INF;
            while(x != s){
                amin(inc, ed[trace[x]].f);
                x = ed[trace[x]].u;
            }
            x = t;
            while(x != s){
                ed[trace[x]].f -= inc;
                ed[trace[x] ^ 1].f += inc;
                x = ed[trace[x]].u;
            }
            res += (ll)dd[t]*inc;
        }
        return res;
    }
};

int n, m;
int b[MAX];
ll res;

void process(){
    cin >> n >> m;
    mincost mc(n + 2, 0, n + 1);
    for(int i = 1; i <= m; i++){
        int u, v, c, f;
        cin >> u >> v >> c >> f;
        b[u] += f;
        b[v] -= f;
        mc.add(u, v, INF, 2);
        if(c < f){
            res += f - c;
            mc.add(v, u, f - c, 0);
            mc.add(v, u, c, 1);
        }else{
            mc.add(u, v, c - f, 1);
            mc.add(v, u, f, 1);
        }
    }

    for(int i = 1; i <= n; i++){
        if(b[i] < 0){
            mc.add(0, i, -b[i], 0);
        }else if(b[i] > 0){
            mc.add(i, n + 1, b[i], 0);
        }
    }
    mc.add(n, 1, INF, 0);
    cout << res + mc.flow();
}

int main(){
    ios_base::sync_with_stdio(false);
    //~ cin.tie(nullptr);
    //~ freopen("d.in", "r", stdin);
    process();
}