//code in https://github.com/kcm1700/algorithms by kcm1700
#include <functional>
#include <queue>
#include <limits>
#include <vector>
#include <algorithm>

using namespace std;

// tests
// Kaka's matrix travel (POJ)
// https://www.acmicpc.net/problem/8992
// SRM 506 mid
// ACM-ICPC World Finals 2015 C Catering

// precondition: there is no negative cycle.
// usage:
//  MinCostFlow mcf(n);
//  for(each edges) mcf.addEdge(from, to, cost, capacity);
//  mcf.solve(source, sink); // min cost max flow
//  mcf.solve(source, sink, 0); // min cost flow
//  mcf.solve(source, sink, goal_flow); // min cost flow with total_flow >= goal_flow if possible
struct MinCostFlow
{
	typedef long long cap_t;
	typedef long long cost_t;

	bool iszerocap(cap_t cap) { return cap == 0; }

	struct edge {
		int target;
		cost_t cost;
		cap_t residual_capacity;
		cap_t orig_capacity;
		size_t revid;
	};

	int n;
	vector<vector<edge>> graph;
	vector<cost_t> pi;
	bool needNormalize, ranbefore;
	int lastStart;

	MinCostFlow(int n) : graph(n), n(n), pi(n, 0), needNormalize(false), ranbefore(false) {}
	void addEdge(int s, int e, cost_t cost, cap_t cap)
	{
		if (s == e) return;
		edge forward={e, cost, cap, cap, graph[e].size()};
		edge backward={s, -cost, 0, 0, graph[s].size()};
		if (cost < 0 || ranbefore) needNormalize = true;
		graph[s].emplace_back(forward);
		graph[e].emplace_back(backward);
	}
	bool normalize(int s) {
		auto infinite_cost = numeric_limits<cost_t>::max();
		vector<cost_t> dist(n, infinite_cost);
		dist[s] = 0;
		queue<int> q;
		vector<int> v(n), relax_count(n);
		v[s] = 1; q.push(s);
		while(!q.empty()) {
			int cur = q.front();
			v[cur] = 0; q.pop();
			if (++relax_count[cur] >= n) return false;
			for (const auto &e : graph[cur]) {
				if (iszerocap(e.residual_capacity)) continue;
				auto next = e.target;
				auto ncost = dist[cur] + e.cost;
				if (dist[next] > ncost) {
					dist[next] = ncost;
					if (v[next]) continue;
					v[next] = 1; q.push(next);
				}
			}
		}
		for (int i = 0; i < n; i++) pi[i] = dist[i];
		return true;
	}

	pair<cost_t, cap_t> AugmentShortest(int s, int e, cap_t flow_limit) {
		auto infinite_cost = numeric_limits<cost_t>::max();
		auto infinite_flow = numeric_limits<cap_t>::max();
		typedef pair<cost_t, int> pq_t;
		priority_queue<pq_t, vector<pq_t>, greater<pq_t>> pq;
		vector<pair<cost_t, cap_t>> dist(n, make_pair(infinite_cost, 0));
		vector<int> from(n, -1), v(n);

		if (needNormalize || (ranbefore && lastStart != s))
			normalize(s);
		ranbefore = true;
		lastStart = s;

		dist[s] = pair<cost_t, cap_t>(0, infinite_flow);
		pq.emplace(dist[s].first, s);
		while(!pq.empty()) {
			auto cur = pq.top().second; pq.pop();
			if (v[cur]) continue;
			v[cur] = 1;
			if (cur == e) continue;
			for (const auto &e : graph[cur]) {
				auto next = e.target;
				if (v[next]) continue;
				if (iszerocap(e.residual_capacity)) continue;
				auto ncost = dist[cur].first + e.cost - pi[next] + pi[cur];
				auto nflow = min(dist[cur].second, e.residual_capacity);
				if (dist[next].first <= ncost) continue;
				dist[next] = make_pair(ncost, nflow);
				from[next] = e.revid;
				pq.emplace(dist[next].first, next);
			}
		}
		/** augment the shortest path **/
		auto p = e;
		auto pathcost = dist[p].first + pi[p] - pi[s];
		auto flow = dist[p].second;
		if (iszerocap(flow)|| (flow_limit <= 0 && pathcost >= 0)) return pair<cost_t, cap_t>(0, 0);
		if (flow_limit > 0) flow = min(flow, flow_limit);
		/* update potential */
		for (int i = 0; i < n; i++) {
			if (iszerocap(dist[i].second)) continue;
			pi[i] += dist[i].first;
		}
		while (from[p] != -1) {
			auto nedge = from[p];
			auto np = graph[p][nedge].target;
			auto fedge = graph[p][nedge].revid;
			graph[p][nedge].residual_capacity += flow;
			graph[np][fedge].residual_capacity -= flow;
			p = np;
		}
		return make_pair(pathcost * flow, flow);
	}

	pair<cost_t,cap_t> solve(int s, int e, cap_t flow_minimum = numeric_limits<cap_t>::max()) {
		cost_t total_cost = 0;
		cap_t total_flow = 0;
		for(;;) {
			auto res = AugmentShortest(s, e, flow_minimum - total_flow);
			if (res.second <= 0) break;
			total_cost += res.first;
			total_flow += res.second;
		}
		return make_pair(total_cost, total_flow);
	}
};

int n,m;
int eg[110][4];
long long int fsum[110];
long long int sans;
int main(){
	int i;
	scanf("%d%d",&n,&m);
	MinCostFlow mcf(n+3);
	sans=0;
	for(i=0;i<m;i++){
		scanf("%d%d%d%d",&eg[i][0],&eg[i][1],&eg[i][2],&eg[i][3]);
		fsum[eg[i][0]]-=eg[i][3];
		fsum[eg[i][1]]+=eg[i][3];
		if(eg[i][2]-eg[i][3]>=0){
			mcf.addEdge(eg[i][0],eg[i][1],1,eg[i][2]-eg[i][3]);
			mcf.addEdge(eg[i][0],eg[i][1],2,999999999);
			mcf.addEdge(eg[i][1],eg[i][0],1,eg[i][3]);
		}
		else{
			sans+=eg[i][3]-eg[i][2];
			mcf.addEdge(eg[i][0],eg[i][1],2,999999999);
			mcf.addEdge(eg[i][0],eg[i][1],0,eg[i][3]-eg[i][2]);
			mcf.addEdge(eg[i][1],eg[i][0],1,eg[i][2]);
		}
	}
	mcf.addEdge(n,1,0,999999999);
	for(i=1;i<=n;i++){
		if(fsum[i]<0){
			mcf.addEdge(n+1,i,0,-fsum[i]);
		}
		else{
			mcf.addEdge(i,n+2,0,fsum[i]);
		}
	}
	printf("%I64d",sans + mcf.solve(n+1,n+2).first);
	return 0;
}


#include <functional>
#include <climits>
#include <queue>
#include <limits>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

struct MinCostFlow
{
  typedef long long cap_t;
  typedef long long cost_t;

  bool iszerocap(cap_t cap) { return cap == 0; }

  struct edge {
    int target;
    cost_t cost;
    cap_t residual_capacity;
    cap_t orig_capacity;
    size_t revid;
  };

  int n;
  vector<vector<edge>> graph;
  vector<cost_t> pi;
  bool needNormalize, ranbefore;
  int lastStart;

  MinCostFlow(int n) : graph(n), n(n), pi(n, 0), needNormalize(false), ranbefore(false) {}
  void addEdge(int s, int e, cost_t cost, cap_t cap)
  {
    if (s == e) return;
    edge forward = { e, cost, cap, cap, graph[e].size() };
    edge backward = { s, -cost, 0, 0, graph[s].size() };
    if (cost < 0 || ranbefore) needNormalize = true;
    graph[s].emplace_back(forward);
    graph[e].emplace_back(backward);
  }
  bool normalize(int s) {
    auto infinite_cost = numeric_limits<cost_t>::max();
    vector<cost_t> dist(n, infinite_cost);
    dist[s] = 0;
    queue<int> q;
    vector<int> v(n), relax_count(n);
    v[s] = 1; q.push(s);
    while (!q.empty()) {
      int cur = q.front();
      v[cur] = 0; q.pop();
      if (++relax_count[cur] >= n) return false;
      for (const auto &e : graph[cur]) {
        if (iszerocap(e.residual_capacity)) continue;
        auto next = e.target;
        auto ncost = dist[cur] + e.cost;
        if (dist[next] > ncost) {
          dist[next] = ncost;
          if (v[next]) continue;
          v[next] = 1; q.push(next);
        }
      }
    }
    for (int i = 0; i < n; i++) pi[i] = dist[i];
    return true;
  }

  pair<cost_t, cap_t> AugmentShortest(int s, int e, cap_t flow_limit) {
    auto infinite_cost = numeric_limits<cost_t>::max();
    auto infinite_flow = numeric_limits<cap_t>::max();
    typedef pair<cost_t, int> pq_t;
    priority_queue<pq_t, vector<pq_t>, greater<pq_t>> pq;
    vector<pair<cost_t, cap_t>> dist(n, make_pair(infinite_cost, 0));
    vector<int> from(n, -1), v(n);

    if (needNormalize || (ranbefore && lastStart != s))
      normalize(s);
    ranbefore = true;
    lastStart = s;

    dist[s] = pair<cost_t, cap_t>(0, infinite_flow);
    pq.emplace(dist[s].first, s);
    while (!pq.empty()) {
      auto cur = pq.top().second; pq.pop();
      if (v[cur]) continue;
      v[cur] = 1;
      if (cur == e) continue;
      for (const auto &e : graph[cur]) {
        auto next = e.target;
        if (v[next]) continue;
        if (iszerocap(e.residual_capacity)) continue;
        auto ncost = dist[cur].first + e.cost - pi[next] + pi[cur];
        auto nflow = min(dist[cur].second, e.residual_capacity);
        if (dist[next].first <= ncost) continue;
        dist[next] = make_pair(ncost, nflow);
        from[next] = e.revid;
        pq.emplace(dist[next].first, next);
      }
    }
    /** augment the shortest path **/
    auto p = e;
    auto pathcost = dist[p].first + pi[p] - pi[s];
    auto flow = dist[p].second;
    if (iszerocap(flow) || (flow_limit <= 0 && pathcost >= 0)) return pair<cost_t, cap_t>(0, 0);
    if (flow_limit > 0) flow = min(flow, flow_limit);
    /* update potential */
    for (int i = 0; i < n; i++) {
      if (iszerocap(dist[i].second)) continue;
      pi[i] += dist[i].first;
    }
    while (from[p] != -1) {
      auto nedge = from[p];
      auto np = graph[p][nedge].target;
      auto fedge = graph[p][nedge].revid;
      graph[p][nedge].residual_capacity += flow;
      graph[np][fedge].residual_capacity -= flow;
      p = np;
    }
    return make_pair(pathcost * flow, flow);
  }

  pair<cost_t, cap_t> solve(int s, int e, cap_t flow_minimum = numeric_limits<cap_t>::max()) {
    cost_t total_cost = 0;
    cap_t total_flow = 0;
    for (;;) {
      auto res = AugmentShortest(s, e, flow_minimum - total_flow);
      if (res.second <= 0) break;
      total_cost += res.first;
      total_flow += res.second;
    }
    return make_pair(total_cost, total_flow);
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<long long> net_vsum(n);
  int source = n, sink = n + 1;
  MinCostFlow mcf(n + 2);
  long long baseval = 0;
  for (int i = 0; i < m; i++) {
    int u, v, c, f;
    scanf("%d%d%d%d", &u, &v, &c, &f);
    u--, v--;
    net_vsum[u] -= f;
    net_vsum[v] += f;
    if (f <= c) {
      mcf.addEdge(u, v, 1, c - f);
      mcf.addEdge(u, v, 2, LLONG_MAX / 100);
      mcf.addEdge(v, u, 1, f);
    }
    else {
      baseval += f - c;
      mcf.addEdge(v, u, 0, f - c);
      mcf.addEdge(u, v, 2, LLONG_MAX / 100);
      mcf.addEdge(v, u, 1, c);
    }
  }
  for (int i = 0; i < n; i++) {
    if (net_vsum[i] > 0) {
      mcf.addEdge(source, i, 0, net_vsum[i]);
    }
    else if (net_vsum[i] < 0) {
      mcf.addEdge(i, sink, 0, -net_vsum[i]);
    }
  }
  mcf.addEdge(n - 1, 0, 0, LLONG_MAX / 100);
  long long ans = mcf.solve(source, sink).first;
  ans += baseval;
  printf("%lld\n", ans);
  return 0;
}


//试一下CodeForces的格式
#include<bits/stdc++.h>
using namespace std;

template <class T> void read(T &x) {
    x = 0; bool flag = 0; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) flag |= ch == '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; flag ? x = ~x + 1 : 0;
}

#define N 110
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define INF 0x3f3f3f3f

int S, T, flow, cost, head[N], tot = 1, dis[N], pre[N];
struct { int v, c, w, next; }e[100001];
queue<int> q;
bool inq[N];
inline void insert(int u, int v, int c, int w) {
    e[++tot].v = v, e[tot].c = c, e[tot].w = w, e[tot].next = head[u], head[u] = tot;
}
inline void add(int u, int v, int c, int w) {
    insert(u, v, c, w), insert(v, u, 0, -w);
}
bool spfa() {
    memset(dis, 0x3f, sizeof dis); dis[S] = 0, q.push(S);
    while (!q.empty()) {
        int u = q.front(); q.pop(), inq[u] = 0;
        for (int i = head[u], v; i; i = e[i].next) e[i].c && dis[v = e[i].v] > dis[u] + e[i].w ?
            dis[v] = dis[u] + e[i].w, pre[v] = i, (!inq[v] ? q.push(v), inq[v] = 1 : 0) : 0;
    }
    return dis[T] < INF;
}
void mcf() {
    int d = INF;
    for (int i = T; i != S; i = e[pre[i] ^ 1].v) d = min(d, e[pre[i]].c);
    for (int i = T; i != S; i = e[pre[i] ^ 1].v) e[pre[i]].c -= d, e[pre[i] ^ 1].c += d, cost += d * e[pre[i]].w;
    flow += d;
}

int d[N];

int main() {
    int n, m; read(n), read(m);
    T = n + 1;
    int ans = 0;
    rep(i, 1, m) {
        int u, v, c, f; read(u), read(v), read(c), read(f);
        d[u] -= f, d[v] += f;
        add(u, v, INF, 2);
        if (f >= c) ans += f - c, add(v, u, f - c, 0), add(v, u, c, 1);
        else add(v, u, f, 1), add(u, v, c - f, 1);
    }
    rep(i, 1, n) d[i] > 0 ? add(S, i, d[i], 0) : add(i, T, -d[i], 0);
    add(n, 1, INF, 0);
    while (spfa()) mcf();
    cout << ans + cost;
    return 0;
}


#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200007;
const int inf=1e9;
bool v[N];
int dis[N];
int n,s,t,res=0,ans=0;
int tot=1;
int head[N],next[N<<1],to[N<<1],edge_c[N<<1],edge_cc[N<<1];
inline void add(register int x,register int y,register int z,register int zz)
{
    next[++tot]=head[x],head[x]=tot,to[tot]=y;
    edge_c[tot]=z;edge_cc[tot]=zz;
}
inline void Add(register int x,register int y,register int z,register int zz)
{
    add(x,y,z,zz);
    add(y,x,0,-zz);
}
inline bool spfa()
{
    memset(v,0,sizeof(v));
    for(register int i=0;i<=t;i++)dis[i]=inf;
    dis[s]=0;v[s]=1;
    deque<int>q;
    q.push_back(s);
    while(q.size())
    {
        register int x=q.front();
        q.pop_front();
        v[x]=0;
        for(register int i=head[x];i;i=next[i])
        {
            register int y=to[i];
            if(edge_c[i]>0&&dis[x]+edge_cc[i]<dis[y])
            {
                dis[y]=dis[x]+edge_cc[i];
                if(!v[y])
                {
                    v[y]=1;
                    if(!q.empty()&&dis[y]<dis[q.front()])q.push_front(y);
                    else q.push_back(y);
                }
            }
        }
    }
    return dis[t]<inf;
}
int dfs(register int x,register int low)
{
    v[x]=1;
    if(x==t)
    {
        res+=low;
        return low;
    }
    register int used=0,delta=0;
    for(register int i=head[x];i;i=next[i])
    {
        register int y=to[i];
        if(!v[y]&&edge_c[i]&&dis[y]==dis[x]+edge_cc[i])
        {
            delta=dfs(y,min(edge_c[i],low-used));
            ans+=delta*edge_cc[i],edge_c[i]-=delta,edge_c[i^1]+=delta,used+=delta;
            if(used==low)break;
        }
    }
    return used;
}
void costflow()
{
    res=ans=0;
    while(spfa())
    {
        v[t]=1;
        while(v[t])
        {
            memset(v,0,sizeof(v));
            dfs(s,inf);
        }
    }
}
int m=0,ss=0,tt=0;
int totflow[N];
int ANS=0;
int main()
{
	scanf("%d%d",&n,&m);
	s=0,t=n+1;
//	ss=n+2;tt=n+3;
	for(register int i=1;i<=m;i++)
	{
		register int x=0,y=0,c=0,f=0;
		scanf("%d%d%d%d",&x,&y,&c,&f);
		totflow[x]-=f;totflow[y]+=f;
		Add(x,y,inf,2);
		if(f>=c)
		{
			ANS+=f-c;
			Add(y,x,f-c,0);
			Add(y,x,c,1);
		}
		else
		{
			Add(x,y,c-f,1);
			Add(y,x,f,1);
		}
	}
	for(register int i=1;i<=n;i++)
	{
		if(totflow[i]>0)
		{
			Add(s,i,totflow[i],0);
		}
		else
		{
			Add(i,t,-totflow[i],0);
		}
	}
	Add(n,1,inf,0);
	costflow();
	printf("%d\n",ANS+ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define R register
#define I inline
#define B bool
#define V void
#define ll long long
I int read()
{
    R int f=1,num=0;
    R char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
    return num*f;
}
const int N=200007;
const int inf=1e9;
bool v[N];
int dis[N];
int n,s,t,res=0,ans=0;
int tot=1;
int HEAD[N],NEXT[N<<1],TO[N<<1],edge_c[N<<1],edge_cc[N<<1];
inline void add(int x,int y, int z, int zz)
{
    NEXT[++tot]=HEAD[x],HEAD[x]=tot,TO[tot]=y;
    edge_c[tot]=z;edge_cc[tot]=zz;
}
inline void Add( int x, int y, int z, int zz)
{
    add(x,y,z,zz);
    add(y,x,0,-zz);
}
inline bool spfa()
{
    memset(v,0,sizeof(v));
    for( int i=0;i<=t;i++)dis[i]=inf;
    dis[s]=0;v[s]=1;
    deque<int>q;
    q.push_back(s);
    while(q.size())
    {
         int x=q.front();
        q.pop_front();
        v[x]=0;
        for( int i=HEAD[x];i;i=NEXT[i])
        {
             int y=TO[i];
            if(edge_c[i]>0&&dis[x]+edge_cc[i]<dis[y])
            {
                dis[y]=dis[x]+edge_cc[i];
                if(!v[y])
                {
                    v[y]=1;
                    if(!q.empty()&&dis[y]<dis[q.front()])q.push_front(y);
                    else q.push_back(y);
                }
            }
        }
    }
    return dis[t]<inf;
}
int dfs( int x, int low)
{
    v[x]=1;
    if(x==t)
    {
        res+=low;
        return low;
    }
     int used=0,delta=0;
    for( int i=HEAD[x];i;i=NEXT[i])
    {
         int y=TO[i];
        if(!v[y]&&edge_c[i]&&dis[y]==dis[x]+edge_cc[i])
        {
            delta=dfs(y,min(edge_c[i],low-used));
            ans+=delta*edge_cc[i],edge_c[i]-=delta,edge_c[i^1]+=delta,used+=delta;
            if(used==low)break;
        }
    }
    return used;
}
void costflow()
{
    res=ans=0;
    while(spfa())
    {
        v[t]=1;
        while(v[t])
        {
            memset(v,0,sizeof(v));
            dfs(s,inf);
        }
    }
}
int m=0,ss=0,tt=0;
int totflow[N];
int ANS=0;
int main()
{
	n=read(),m=read();
	s=0,t=n+1;
//	ss=n+2;tt=n+3;
	for( int i=1;i<=m;i++)
	{
		int x=read(),y=read(),c=read(),f=read();
		totflow[x]-=f;totflow[y]+=f;
		Add(x,y,inf,2);
		if(f>=c)
		{
			ANS+=f-c;
			Add(y,x,f-c,0);
			Add(y,x,c,1);
		}
		else
		{
			Add(x,y,c-f,1);
			Add(y,x,f,1);
		}
	}
	for( int i=1;i<=n;i++)
	{
		if(totflow[i]>0)
		{
			Add(s,i,totflow[i],0);
		}
		else
		{
			Add(i,t,-totflow[i],0);
		}
	}
	Add(n,1,inf,0);
	costflow();
	printf("%d\n",ANS+ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define R register
#define I inline
#define B bool
#define V void
#define ll long long
I int read()
{
    R int f=1,num=0;
    R char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
    return num*f;
}
const int N=200007;
const int inf=1e9;
bool v[N];
int dis[N];
int n,s,t,res=0,ans=0;
int tot=1;
int head[N],next[N<<1],to[N<<1],edge_c[N<<1],edge_cc[N<<1];
inline void add(int x,int y, int z, int zz)
{
    next[++tot]=head[x],head[x]=tot,to[tot]=y;
    edge_c[tot]=z;edge_cc[tot]=zz;
}
inline void Add( int x, int y, int z, int zz)
{
    add(x,y,z,zz);
    add(y,x,0,-zz);
}
inline bool spfa()
{
    memset(v,0,sizeof(v));
    for( int i=0;i<=t;i++)dis[i]=inf;
    dis[s]=0;v[s]=1;
    deque<int>q;
    q.push_back(s);
    while(q.size())
    {
         int x=q.front();
        q.pop_front();
        v[x]=0;
        for( int i=head[x];i;i=next[i])
        {
             int y=to[i];
            if(edge_c[i]>0&&dis[x]+edge_cc[i]<dis[y])
            {
                dis[y]=dis[x]+edge_cc[i];
                if(!v[y])
                {
                    v[y]=1;
                    if(!q.empty()&&dis[y]<dis[q.front()])q.push_front(y);
                    else q.push_back(y);
                }
            }
        }
    }
    return dis[t]<inf;
}
int dfs( int x, int low)
{
    v[x]=1;
    if(x==t)
    {
        res+=low;
        return low;
    }
     int used=0,delta=0;
    for( int i=head[x];i;i=next[i])
    {
         int y=to[i];
        if(!v[y]&&edge_c[i]&&dis[y]==dis[x]+edge_cc[i])
        {
            delta=dfs(y,min(edge_c[i],low-used));
            ans+=delta*edge_cc[i],edge_c[i]-=delta,edge_c[i^1]+=delta,used+=delta;
            if(used==low)break;
        }
    }
    return used;
}
void costflow()
{
    res=ans=0;
    while(spfa())
    {
        v[t]=1;
        while(v[t])
        {
            memset(v,0,sizeof(v));
            dfs(s,inf);
        }
    }
}
int m=0,ss=0,tt=0;
int totflow[N];
int ANS=0;
int main()
{
	n=read(),m=read();
	s=0,t=n+1;
//	ss=n+2;tt=n+3;
	for( int i=1;i<=m;i++)
	{
		int x=read(),y=read(),c=read(),f=read();
		totflow[x]-=f;totflow[y]+=f;
		Add(x,y,inf,2);
		if(f>=c)
		{
			ANS+=f-c;
			Add(y,x,f-c,0);
			Add(y,x,c,1);
		}
		else
		{
			Add(x,y,c-f,1);
			Add(y,x,f,1);
		}
	}
	for( int i=1;i<=n;i++)
	{
		if(totflow[i]>0)
		{
			Add(s,i,totflow[i],0);
		}
		else
		{
			Add(i,t,-totflow[i],0);
		}
	}
	Add(n,1,inf,0);
	costflow();
	printf("%d\n",ANS+ans);
	return 0;
}


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <string.h>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define swap(x,y) x^=y,y^=x,x^=y
#define R register
#define I inline
#define B bool
#define V void
#define ll long long
I int read()
{
    R int f=1,num=0;
    R char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) num=(num<<1)+(num<<3)+ch-'0',ch=getchar();
    return num*f;
}
const int N=200007;
const int inf=1e9;
B v[N];
int dis[N];
int n,s,t,res=0,ans=0;
int tot=1;
int head[N],next[N<<1],to[N<<1],edge_c[N<<1],edge_cc[N<<1];
I V add(R int x,R int y,R int z,R int zz)
{
    next[++tot]=head[x],head[x]=tot,to[tot]=y;
    edge_c[tot]=z;edge_cc[tot]=zz;
}
I V Add(R int x,R int y,R int z,R int zz)
{
    add(x,y,z,zz);
    add(y,x,0,-zz);
}
I B spfa()
{
    memset(v,0,sizeof(v));
    for(R int i=0;i<=t;i++)dis[i]=inf;
    dis[s]=0;v[s]=1;
    deque<int>q;
    q.push_back(s);
    while(q.size())
    {
        R int x=q.front();
        q.pop_front();
        v[x]=0;
        for(R int i=head[x];i;i=next[i])
        {
            R int y=to[i];
            if(edge_c[i]>0&&dis[x]+edge_cc[i]<dis[y])
            {
                dis[y]=dis[x]+edge_cc[i];
                if(!v[y])
                {
                    v[y]=1;
                    if(!q.empty()&&dis[y]<dis[q.front()])q.push_front(y);
                    else q.push_back(y);
                }
            }
        }
    }
    return dis[t]<inf;
}
int dfs(R int x,R int low)
{
    v[x]=1;
    if(x==t)
    {
        res+=low;
        return low;
    }
    R int used=0,delta=0;
    for(R int i=head[x];i;i=next[i])
    {
        R int y=to[i];
        if(!v[y]&&edge_c[i]&&dis[y]==dis[x]+edge_cc[i])
        {
            delta=dfs(y,min(edge_c[i],low-used));
            ans+=delta*edge_cc[i],edge_c[i]-=delta,edge_c[i^1]+=delta,used+=delta;
            if(used==low)break;
        }
    }
    return used;
}
V costflow()
{
    res=ans=0;
    while(spfa())
    {
        v[t]=1;
        while(v[t])
        {
            memset(v,0,sizeof(v));
            dfs(s,inf);
        }
    }
}
int m=0,ss=0,tt=0;
int totflow[N];
int ANS=0;
int main()
{
	n=read();m=read();
	s=0,t=n+1;
//	ss=n+2;tt=n+3;
	for(R int i=1;i<=m;i++)
	{
		R int x=read(),y=read(),c=read(),f=read();
		totflow[x]-=f;totflow[y]+=f;
		Add(x,y,inf,2);
		if(f>=c)
		{
			ANS+=f-c;
			Add(y,x,f-c,0);
			Add(y,x,c,1);
		}
		else
		{
			Add(x,y,c-f,1);
			Add(y,x,f,1);
		}
	}
	for(R int i=1;i<=n;i++)
	{
		if(totflow[i]>0)
		{
			Add(s,i,totflow[i],0);
		}
		else
		{
			Add(i,t,-totflow[i],0);
		}
	}
	Add(n,1,inf,0);
//	R int tmps=s,tmpt=t;
//	s=ss;t=tt;
	costflow();
	cout<<(ANS+ans)<<endl;
	return 0;
}


#if 0
date

solution
#endif
#include <cstdio>
#include <algorithm>
#include <queue>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;
struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
} read;

const int maxn = 110, maxm = 1010;
int head[maxn], nxt[maxm], to[maxm], cap[maxm], cost[maxm], hp = 1;

inline void add(int u, int v, int ca, int co) {
	if((hp & 1) and !ca) return;
	nxt[++ hp] = head[u];
	head[u] = hp;
	to[hp] = v;
	cap[hp] = ca;
	cost[hp] = co;
	if(!(hp & 1)) add(v, u, 0, - co);
}

int dis[maxn];
bool inq[maxn];

bool spfa(int s, int t) {
	std::queue<int> q;
	std::fill(dis, dis + t + 1, 1000000000);
	std::fill(inq, inq + t + 1, 0);
	dis[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		inq[u] = 0;
		for(int i = head[u]; i; i = nxt[i])
			if(cap[i] and dis[u] + cost[i] < dis[to[i]]) {
				dis[to[i]] = dis[u] + cost[i];
				if(!inq[to[i]]) {
					q.push(to[i]);
					inq[to[i]] = 1;
				}
			}
	}
	return dis[t] < 1000000000;
}

int cur[maxn];
int dinic(int u, int t, int flow) {
	inq[u] = 1;
	if(u == t) return flow;
	for(int &i = cur[u]; i; i = nxt[i])
		if(cap[i] and !inq[to[i]] and dis[u] + cost[i] == dis[to[i]]) {
			int f = dinic(to[i], t, std::min(flow, cap[i]));
			if(f) {
				cap[i] -= f;
				cap[i ^ 1] += f;
				inq[u] = 0;
				return f;
			}
		}
	inq[u] = 0;
	return 0;
}

int mincostmaxflow(int s, int t) {
	int res = 0;
	while(spfa(s, t)) {
		std::copy(head, head + t + 1, cur);
		int f = 0;
		do {
			f = dinic(s, t, 2147483647);
			res += dis[t] * f;
		} while(f);
	}
	return res;
}

int deg[maxn];
int main() {
	int n = read, m = read;
	int ans = 0;
	for(int i = 1; i <= m; i ++) {
		int u = read, v = read, c = read, f = read;
		if(c < f) {
			ans += f - c;
			c = f;
		}
		deg[u] -= f;
		deg[v] += f;
		add(u, v, c - f, 1);
		add(v, u, f, 1);
		add(u, v, 1000000000, 2);
	}

	int s = n + 1, t = n + 2, ss = n + 3, tt = n + 4;
	add(s, 1, 1000000000, 0);
	add(n, t, 1000000000, 0);
	add(t, s, 1000000000, 0);
	for(int i = 1; i <= n; i ++) {
		if(deg[i] > 0) {
			add(ss, i, deg[i], 0);
			add(s, tt, deg[i], 0);
		}
		if(deg[i] < 0) {
			add(ss, t, -deg[i], 0);
			add(i, tt, -deg[i], 0);
		}
	}

	ans += mincostmaxflow(ss, tt);
	printf("%d\n", ans);
}


#if 0
2020.02.01

建立新的网络图，先不考虑容量，对于原来的每条边 u -> v ，
新图中 u -> v 的流量为代表原图中 u -> v 的流量增量，
增量没有上界，因此容量正无穷，但是费用为 1 。
而 v -> u 的流量代表原图中 u -> v 的流量减少量。
减少量有上界，即原图流量 f ，同样费用为 1 。

为了满足流量平衡，对于点 u ，
如果流入流量比流出流量大 x ，说明需要增加出边流量或者减少入边流量，
连边 S -> u ，其中 S 是新建源点，
这条边的流量必须为 x ，上下界都为 x ，没有费用。
同理如果流出流量比流入流量大 x ，连边 u -> T ，上下界为 x ，无费用。
特别的原图的源汇点不需要满足流量平衡，
连边 S -> 1 和 n -> T ，容量正无穷。

这样新图的最小费用可行流就是答案，前提是忽略原图流量限制。

考虑到原图流量限制，对于边 (u, v, c, f) ：
1) c > f
流量增量不超过 c - f 时费用为 1 。
如果流量增量超过 c - f ，每次流量增加的同时需要增加容量，因此费用为 2 。
流量减少量的费用仍然为 1 。
2) c < f
先把答案加上 f - c ，默认容量被加到了 f 。
首先流量增量的费用始终是 2 。
流量减少的话，说明容量没必要增加到 f ，流量减少的同时减少容量，最多减到 c ，
因此流量减少量不超过 f - c 时费用为 0 。
如果流量减少量超过 f - c 费用仍然为 1 。

把需要分段的边拆成两条，这样新图的最小费用可行流加上提到的 f - c 就是答案了。
#endif
#include <cstdio>
#include <algorithm>
#include <queue>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;
struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
} read;

const int maxn = 110, maxm = 1010;
int head[maxn], nxt[maxm], to[maxm], cap[maxm], cost[maxm], hp = 1;

inline void add(int u, int v, int ca, int co) {
	if((hp & 1) and !ca) return;
	nxt[++ hp] = head[u];
	head[u] = hp;
	to[hp] = v;
	cap[hp] = ca;
	cost[hp] = co;
	if(!(hp & 1)) add(v, u, 0, - co);
}

int dis[maxn];
bool inq[maxn];

bool spfa(int s, int t) {
	std::queue<int> q;
	std::fill(dis, dis + t + 1, 1000000000);
	std::fill(inq, inq + t + 1, 0);
	dis[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		inq[u] = 0;
		for(int i = head[u]; i; i = nxt[i])
			if(cap[i] and dis[u] + cost[i] < dis[to[i]]) {
				dis[to[i]] = dis[u] + cost[i];
				if(!inq[to[i]]) {
					q.push(to[i]);
					inq[to[i]] = 1;
				}
			}
	}
	return dis[t] < 1000000000;
}

int cur[maxn];
int dinic(int u, int t, int flow) {
	inq[u] = 1;
	if(u == t) return flow;
	for(int &i = cur[u]; i; i = nxt[i])
		if(cap[i] and !inq[to[i]] and dis[u] + cost[i] == dis[to[i]]) {
			int f = dinic(to[i], t, std::min(flow, cap[i]));
			if(f) {
				cap[i] -= f;
				cap[i ^ 1] += f;
				inq[u] = 0;
				return f;
			}
		}
	inq[u] = 0;
	return 0;
}

int mincostmaxflow(int s, int t) {
	int res = 0;
	while(spfa(s, t)) {
		std::copy(head, head + t + 1, cur);
		int f = 0;
		do {
			f = dinic(s, t, 2147483647);
			res += dis[t] * f;
		} while(f);
	}
	return res;
}

int deg[maxn];
int main() {
	int n = read, m = read;
	int ans = 0;
	for(int i = 1; i <= m; i ++) {
		int u = read, v = read, c = read, f = read;
		if(c < f) {
			ans += f - c;
			add(v, u, f - c, 0);
			add(v, u, c, 1);
			add(u, v, 1000000000, 2);
		} else {
			add(u, v, c - f, 1);
			add(v, u, f, 1);
			add(u, v, 1000000000, 2);
		}
		deg[u] -= f;
		deg[v] += f;
	}

	int s = n + 1, t = n + 2, ss = n + 3, tt = n + 4;
	add(s, 1, 1000000000, 0);
	add(n, t, 1000000000, 0);
	add(t, s, 1000000000, 0);
	for(int i = 1; i <= n; i ++) {
		if(deg[i] > 0) {
			add(ss, i, deg[i], 0);
			add(s, tt, deg[i], 0);
		}
		if(deg[i] < 0) {
			add(ss, t, -deg[i], 0);
			add(i, tt, -deg[i], 0);
		}
	}

	ans += mincostmaxflow(ss, tt);
	printf("%d\n", ans);
}


#if 0
date

solution
#endif
#include <cstdio>
#include <algorithm>
#include <queue>
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;
struct {
	inline operator int () { int x; return scanf("%d", &x), x; }
} read;

const int maxn = 110, maxm = 1010;
int head[maxn], nxt[maxm], to[maxm], cap[maxm], cost[maxm], hp = 1;

inline void add(int u, int v, int ca, int co) {
	if((hp & 1) and !ca) return;
	nxt[++ hp] = head[u];
	head[u] = hp;
	to[hp] = v;
	cap[hp] = ca;
	cost[hp] = co;
	if(!(hp & 1)) add(v, u, 0, - co);
}

int dis[maxn];
bool inq[maxn];

bool spfa(int s, int t) {
	std::queue<int> q;
	std::fill(dis, dis + t + 1, 1000000000);
	std::fill(inq, inq + t + 1, 0);
	dis[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		inq[u] = 0;
		for(int i = head[u]; i; i = nxt[i])
			if(cap[i] and dis[u] + cost[i] < dis[to[i]]) {
				dis[to[i]] = dis[u] + cost[i];
				if(!inq[to[i]]) {
					q.push(to[i]);
					inq[to[i]] = 1;
				}
			}
	}
	return dis[t] < 1000000000;
}

int cur[maxn];
int dinic(int u, int t, int flow) {
	inq[u] = 1;
	if(u == t) return flow;
	for(int &i = cur[u]; i; i = nxt[i])
		if(cap[i] and !inq[to[i]] and dis[u] + cost[i] == dis[to[i]]) {
			int f = dinic(to[i], t, std::min(flow, cap[i]));
			if(f) {
				cap[i] -= f;
				cap[i ^ 1] += f;
				inq[u] = 0;
				return f;
			}
		}
	inq[u] = 0;
	return 0;
}

int mincostmaxflow(int s, int t) {
	int res = 0;
	while(spfa(s, t)) {
		std::copy(head, head + t + 1, cur);
		int f = 0;
		do {
			f = dinic(s, t, 2147483647);
			res += dis[t] * f;
		} while(f);
	}
	return res;
}

int deg[maxn];
int main() {
	int n = read, m = read;
	int ans = 0;
	for(int i = 1; i <= m; i ++) {
		int u = read, v = read, c = read, f = read;
		if(c < f) {
			ans += f - c;
			add(v, u, f - c, 0);
			add(v, u, c, 1);
			add(u, v, 1000000000, 2);
		} else {
			add(u, v, c - f, 1);
			add(v, u, f, 1);
			add(u, v, 1000000000, 2);
		}
		deg[u] -= f;
		deg[v] += f;
	}

	int s = n + 1, t = n + 2, ss = n + 3, tt = n + 4;
	add(s, 1, 1000000000, 0);
	add(n, t, 1000000000, 0);
	add(t, s, 1000000000, 0);
	for(int i = 1; i <= n; i ++) {
		if(deg[i] > 0) {
			add(ss, i, deg[i], 0);
			add(s, tt, deg[i], 0);
		}
		if(deg[i] < 0) {
			add(ss, t, -deg[i], 0);
			add(i, tt, -deg[i], 0);
		}
	}

	ans += mincostmaxflow(ss, tt);
	printf("%d\n", ans);
}


#include<bits/stdc++.h>
#define D(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=1000005,M=1000005,INF=0X3F3F3F3F;
int n,m,mf,mc,S,T;
int pp=1,d[N],lnk[N],dis[N],pre[N],nxt[M],to[M],w[M],c[M];
void jb(int k1,int k2,int k3,int k4){
//	D("jb:%d %d %d %d\n",k1,k2,k3,k4);
	to[++pp]=k2,w[pp]=k3,c[pp]=k4,nxt[pp]=lnk[k1],lnk[k1]=pp;
	to[++pp]=k1,w[pp]=0,c[pp]=-k4,nxt[pp]=lnk[k2],lnk[k2]=pp;
}
void ae(int k1,int k2,int k3,int k4,int k5){
	if(k3<=k4){
		jb(k1,k2,k4-k3,k5);
		d[k1]-=k3,d[k2]+=k3;
		mc+=k3*k5;
	}
}
bool spfa(){
	static bool inq[N];
	rep(i,1,T)dis[i]=INF;dis[S]=0;queue<int>q;q.push(S),inq[S]=1;
	while(q.size()){
		int k1=q.front();q.pop();inq[k1]=0;
		for(int i=lnk[k1];i;i=nxt[i])if(w[i]>0&&dis[k1]+c[i]<dis[to[i]]){
			pre[to[i]]=i;
			dis[to[i]]=dis[k1]+c[i];
			if(!inq[to[i]]){
				inq[to[i]]=1;
				q.push(to[i]);
			}
		}
	}
	if(dis[T]!=INF)return 1;
	return 0;
}
int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,m){
		int k1,k2,k3,k4;scanf("%d%d%d%d",&k1,&k2,&k3,&k4);
		if(k4<=k3){
			mc-=k4;
			ae(k1,k2,k4,k3,1);
			jb(k1,k2,INF,2);
			jb(k2,k1,INF,1);
		}else{
			mc+=k4-k3;
			ae(k1,k2,k3,k4,0);
			jb(k1,k2,INF,2);
			jb(k2,k1,INF,1);
		}
	}
	S=n+1,T=n+2;
	rep(i,1,n){
		if(d[i]>0)jb(S,i,d[i],0);
		if(d[i]<0)jb(i,T,-d[i],0);
	}
	jb(n,1,INF,0);
	while(spfa()){
		int mn=INF;
		for(int i=pre[T];i;i=pre[to[i^1]])mn=min(mn,w[i]);
		for(int i=pre[T];i;i=pre[to[i^1]])w[i]-=mn,w[i^1]+=mn;
		mf+=mn;
		mc+=dis[T]*mn;
	}
	printf("%d\n",mc);
	return 0;
}


#include<bits/stdc++.h>
#define D(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=105,M=1000005,INF=0X3F3F3F3F;
int n,m,mf,mc,S,T;
int pp=1,d[N],lnk[N],dis[N],pre[N],nxt[M],to[M],w[M],c[M];
void jb(int k1,int k2,int k3,int k4){
//	D("jb:%d %d %d %d\n",k1,k2,k3,k4);
	to[++pp]=k2,w[pp]=k3,c[pp]=k4,nxt[pp]=lnk[k1],lnk[k1]=pp;
	to[++pp]=k1,w[pp]=0,c[pp]=-k4,nxt[pp]=lnk[k2],lnk[k2]=pp;
}
void ae(int k1,int k2,int k3,int k4,int k5){
	if(k3<=k4){
		jb(k1,k2,k4-k3,k5);
		d[k1]-=k3,d[k2]+=k3;
		mc+=k3*k5;
	}
}
bool spfa(){
	static bool inq[N];
	rep(i,1,T)dis[i]=INF;dis[S]=0;queue<int>q;q.push(S),inq[S]=1;
	while(q.size()){
		int k1=q.front();q.pop();inq[k1]=0;
		for(int i=lnk[k1];i;i=nxt[i])if(w[i]>0&&dis[k1]+c[i]<dis[to[i]]){
			pre[to[i]]=i;
			dis[to[i]]=dis[k1]+c[i];
			if(!inq[to[i]]){
				inq[to[i]]=1;
				q.push(to[i]);
			}
		}
	}
	if(dis[T]!=INF)return 1;
	return 0;
}
int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,m){
		int k1,k2,k3,k4;scanf("%d%d%d%d",&k1,&k2,&k3,&k4);
		if(k4<=k3){
			mc-=k4;
			ae(k1,k2,k4,k3,1);
			jb(k1,k2,INF,2);
			jb(k2,k1,INF,1);
		}else{
			mc+=k4-k3;
			ae(k1,k2,k3,k4,0);
			jb(k1,k2,INF,2);
			jb(k2,k1,INF,1);
		}
	}
	S=n+1,T=n+2;
	rep(i,1,n){
		if(d[i]>0)jb(S,i,d[i],0);
		if(d[i]<0)jb(i,T,-d[i],0);
	}
	jb(n,1,INF,0);
	while(spfa()){
		int mn=INF;
		for(int i=pre[T];i;i=pre[to[i^1]])mn=min(mn,w[i]);
		for(int i=pre[T];i;i=pre[to[i^1]])w[i]-=mn,w[i^1]+=mn;
		mf+=mn;
		mc+=dis[T]*mn;
	}
	printf("%d\n",mc);
	return 0;
}


#include<bits/stdc++.h>
#define D(...) fprintf(stderr,__VA_ARGS__)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N=1000005,M=1000005,INF=0X3F3F3F3F;
int n,m,mf,mc,S,T;
int pp=1,d[N],lnk[N],dis[N],pre[N],nxt[M],to[M],w[M],c[M];
void jb(int k1,int k2,int k3,int k4){
//	D("jb:%d %d %d %d\n",k1,k2,k3,k4);
	to[++pp]=k2,w[pp]=k3,c[pp]=k4,nxt[pp]=lnk[k1],lnk[k1]=pp;
	to[++pp]=k1,w[pp]=0,c[pp]=-k4,nxt[pp]=lnk[k2],lnk[k2]=pp;
}
void ae(int k1,int k2,int k3,int k4,int k5){
	if(k3<=k4){
		jb(k1,k2,k4-k3,k5);
		d[k1]-=k3,d[k2]+=k3;
		mc+=k3*k5;
	}
}
bool spfa(){
	static bool inq[N];
	rep(i,1,T)dis[i]=INF;dis[S]=0;queue<int>q;q.push(S),inq[S]=1;
	while(q.size()){
		int k1=q.front();q.pop();inq[k1]=0;
		for(int i=lnk[k1];i;i=nxt[i])if(w[i]>0&&dis[k1]+c[i]<dis[to[i]]){
			pre[to[i]]=i;
			dis[to[i]]=dis[k1]+c[i];
			if(!inq[to[i]]){
				inq[to[i]]=1;
				q.push(to[i]);
			}
		}
	}
	if(dis[T]!=INF)return 1;
	return 0;
}
int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,m){
		int k1,k2,k3,k4;scanf("%d%d%d%d",&k1,&k2,&k3,&k4);
		if(k4<=k3){
			mc-=k4;
			ae(k1,k2,k4,k3,1);
			jb(k1,k2,INF,2);
			jb(k2,k1,k4,1);
		}else{
			mc+=k4-k3;
			ae(k1,k2,k3,k4,0);
			jb(k1,k2,INF,2);
			jb(k2,k1,k3,1);
		}
	}
	S=n+1,T=n+2;
	rep(i,1,n){
		if(d[i]>0)jb(S,i,d[i],0);
		if(d[i]<0)jb(i,T,-d[i],0);
	}
	jb(n,1,INF,0);
	while(spfa()){
		int mn=INF;
		for(int i=pre[T];i;i=pre[to[i^1]])mn=min(mn,w[i]);
		for(int i=pre[T];i;i=pre[to[i^1]])w[i]-=mn,w[i^1]+=mn;
		mf+=mn;
		mc+=dis[T]*mn;
	}
	printf("%d\n",mc);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top;
ll dis[N],he[N],vis[N],ins[N],stk[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		if(x==T) return 1;
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			stk[++top]=i;
			if(vis[v]==tim) return 1;
			else
			{
				dis[v]=dis[x]+f;
				if(dfs(v)==1) return 1;
			}
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(top==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=top;i++) nfl=min(nfl,e[stk[i]].w),nvl+=e[stk[i]].f;
		ans+=nfl*nvl;
		for(i=1;i<=top;i++) e[stk[i]].w-=nfl,e[stk[i]^1].w+=nfl;
	}
	while(spfa()&&dis[T]<0)
	{
		ans+=dinic(S,inf)*dis[T];
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,inf,2);
			eadd(u,v,c-f,1);
			eadd(u,v,f,-1);
		}
		else
		{
			eadd(u,v,inf,2);
			eadd(u,v,f-c,0);
			eadd(u,v,c,-1);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		if(n==12&&m==70) puts("OK");
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}

	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		if(n==12&&m==70) puts("OK");
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}

	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num,W;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>=W&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,inf,2);
			eadd(u,v,c-f,1);
			eadd(u,v,f,-1);
		}
		else
		{
			eadd(u,v,inf,2);
			eadd(u,v,f-c,0);
			eadd(u,v,c,-1);
		}
	}
	//printf("P%lld\n",ans);
	for(W=1e6;W>0;W>>=1)
	{
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		//if(n==12&&m==70) puts("OK");
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	}
	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
		while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num,W;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>=W&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,inf,2);
			eadd(u,v,c-f,1);
			eadd(u,v,f,-1);
		}
		else
		{
			eadd(u,v,inf,2);
			eadd(u,v,f-c,0);
			eadd(u,v,c,-1);
		}
	}
	//printf("P%lld\n",ans);
	for(W=1e6;W>0;W>>=1)
	{
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		if(n==12&&m==70) puts("OK");
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	}
	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num,W;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>=W&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				while(true)
				{
					h[++num]=stk[top--];
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,inf,2);
			eadd(u,v,c-f,1);
			eadd(u,v,f,-1);
		}
		else
		{
			eadd(u,v,inf,2);
			eadd(u,v,f-c,0);
			eadd(u,v,c,-1);
		}
	}
	for(W=1e6;W>0;W>>=1)
	{
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	}
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top;
ll dis[N],he[N],vis[N],ins[N],stk[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		if(x==T) return 1;
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			stk[++top]=i;
			if(vis[v]==tim) return 1;
			else
			{
				dis[v]=dis[x]+f;
				if(dfs(v)==1) return 1;
			}
			top--;
		}
	}
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	while(true)
	{
		top=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(top==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=top;i++) nfl=min(nfl,e[stk[i]].w),nvl+=e[stk[i]].f;
		ans+=nfl*nvl;
		for(i=1;i<=top;i++) e[stk[i]].w-=nfl,e[stk[i]^1].w+=nfl;
	}
	while(spfa()&&dis[T]<0)
	{
		ans+=dinic(S,inf)*dis[T];
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top;
ll dis[N],he[N],vis[N],ins[N],stk[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			stk[++top]=i;
			if(vis[v]==tim) return 1;
			else
			{
				dis[v]=dis[x]+f;
				if(dfs(v)==1) return 1;
			}
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(top==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=top;i++) nfl=min(nfl,e[stk[i]].w),nvl+=e[stk[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		ans+=nfl*nvl;
		for(i=1;i<=top;i++) e[stk[i]].w-=nfl,e[stk[i]^1].w+=nfl;
	}
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top,num;
ll dis[N],he[N],vis[N],ins[N],stk[N],h[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	//printf("U%lld\n",x);
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			dis[v]=dis[x]+f;
			stk[++top]=i;
			if(vis[v]==tim)
			{
				num=0;
				//printf("CV%lld\n",v);
				//for(i=1;i<=top;i++) printf("%lld ",e[stk[i]^1].v);
				//putchar('\n');
				while(true)
				{
					h[++num]=stk[top--];
					//printf("S%lld  %lld %lld\n",top+1,e[stk[top+1]^1].v,v);
					if(e[stk[top+1]^1].v==v) break;
				}
				return 1;
			}
			else if(dfs(v)==1) return 1;
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;num=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(num==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=num;i++) nfl=min(nfl,e[h[i]].w),nvl+=e[h[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		//for(i=1;i<=num;i++) printf("%lld ",e[h[i]].v);
		//putchar('\n');
		ans+=nfl*nvl;
		for(i=1;i<=num;i++) e[h[i]].w-=nfl,e[h[i]^1].w+=nfl;
	}
	if(n==12&&m==70) puts("OK");
	//puts("dinic");
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=150,E=N*10,inf=1e9;
ll n,m,S,T,cnt=1,ans=0,tim,top;
ll dis[N],he[N],vis[N],ins[N],stk[N];
queue<ll> que;
struct Edge
{
	ll v,w,f,nxt;
}e[E];
void eadd(ll u,ll v,ll w,ll f)
{
	//printf("edge%lld %lld %lld %lld\n",u,v,w,f);
	cnt++;e[cnt].v=v;e[cnt].w=w;e[cnt].f=f;e[cnt].nxt=he[u];he[u]=cnt;
	cnt++;e[cnt].v=u;e[cnt].w=0;e[cnt].f=-f;e[cnt].nxt=he[v];he[v]=cnt;
}
bool spfa()
{
	ll x,i,v,w,f;
	memset(vis,0,sizeof(vis));
	memset(ins,0,sizeof(ins));
	memset(dis,0x3f,sizeof(dis));dis[S]=0;
	while(!que.empty()) que.pop();que.push(S);ins[S]=1;
	while(!que.empty())
	{
		x=que.front();que.pop();ins[x]=0;
		//printf("P%lld %lld\n",x,dis[x]);
		for(i=he[x];i;i=e[i].nxt)
		{
			v=e[i].v;w=e[i].w;f=e[i].f;
			//printf("V%lld %lld %lld\n",v,w,f);
			if(w==0) continue;
			if(dis[x]+f<dis[v])
			{
				dis[v]=dis[x]+f;
				//puts("OK");
				if(!ins[v]) ins[v]=1,que.push(v);
			}
		}
	}
	if(dis[T]<0) return 1;
	else return 0;
}
ll dinic(ll x,ll flow)
{
	if(x==T) return flow;
	ll i,v,w,f,nfl,rest=flow;
	vis[x]=1;
	for(i=he[x];i&&rest;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(dis[x]+f==dis[v]&&vis[v]==0&&w>0)
		{
			nfl=dinic(v,min(w,rest));
			e[i].w-=nfl;e[i^1].w+=nfl;rest-=nfl;
		}
	}
	return flow-rest;
}
bool dfs(ll x)
{
	ll i,v,w,f;
	vis[x]=tim;
	for(i=he[x];i;i=e[i].nxt)
	{
		v=e[i].v;w=e[i].w;f=e[i].f;
		if(w>0&&dis[x]+f<dis[v])
		{
			stk[++top]=i;
			if(vis[v]==tim) return 1;
			else
			{
				dis[v]=dis[x]+f;
				if(dfs(v)==1) return 1;
			}
			top--;
		}
	}
	vis[x]=0;
	return 0;
}
int main()
{
	ios::sync_with_stdio(false);
	ll i,u,v,c,f,nfl,nvl;
	cin>>n>>m;
	S=1;T=n;
	for(i=1;i<=m;i++)
	{
		cin>>u>>v>>c>>f;
		ans+=f;
		if(f<=c)
		{
			eadd(u,v,f,-1);
			eadd(u,v,c-f,1);
			eadd(u,v,inf,2);
		}
		else
		{
			eadd(u,v,c,-1);
			eadd(u,v,f-c,0);
			eadd(u,v,inf,2);
		}
	}
	//printf("P%lld\n",ans);
	while(true)
	{
		top=0;
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
		{
			tim++;
			if(dfs(i)) break;
		}
		if(top==0) break;
		nfl=inf;nvl=0;
		for(i=1;i<=top;i++) nfl=min(nfl,e[stk[i]].w),nvl+=e[stk[i]].f;
		//printf("U%lld %lld\n",nfl,nvl);
		ans+=nfl*nvl;
		for(i=1;i<=top;i++) e[stk[i]].w-=nfl,e[stk[i]^1].w+=nfl;
	}
	while(spfa())
	{
		ans+=dinic(S,inf)*dis[T];
		while(true)
		{
			top=0;
			memset(dis,0,sizeof(dis));
			for(i=1;i<=n;i++)
			{
				tim++;
				if(dfs(i)) break;
			}
			if(top==0) break;
			nfl=inf;nvl=0;
			for(i=1;i<=top;i++) nfl=min(nfl,e[stk[i]].w),nvl+=e[stk[i]].f;
			//printf("U%lld %lld\n",nfl,nvl);
			ans+=nfl*nvl;
			for(i=1;i<=top;i++) e[stk[i]].w-=nfl,e[stk[i]^1].w+=nfl;
		}
		//printf("S%lld\n",dis[T]);
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=105,M=1e5+10,INF=1e9;
int ans,cnt,tot=1,S,T,n,m,pre[N],dis[N],de[N],mark[N],head[N],to[M],ne[M],w[M],c[M];
void add(int x,int y,int l,int r)
{
		to[++tot]=y;
		w[tot]=l;
		c[tot]=r;
		ne[tot]=head[x];
		head[x]=tot;
}
void link(int x,int y,int l,int r,int z)
{
		de[x]-=l,de[y]+=l;
		add(x,y,r-l,z),add(y,x,0,-z);
}
bool Spfa()
{
		for(int i=1;i<=cnt;i++) dis[i]=INF;
		queue<int>q;
		q.push(S);
		dis[S]=0;
		while(q.size())
		{
				int x=q.front();
				q.pop();
				mark[x]=0;
				for(int i=head[x];i;i=ne[i])
				{
						int y=to[i];
						if(!w[i]) continue;
						if(dis[y]>dis[x]+c[i])
						{
								dis[y]=dis[x]+c[i];
								pre[y]=i;
								if(!mark[y]) mark[y]=1,q.push(y);
						}
				}
		}
		return dis[T]!=INF;
}
void Ek()
{
		int flow=INF;
		for(int i=T;i!=S;i=to[pre[i]^1]) flow=min(flow,w[pre[i]]);
		for(int i=T;i!=S;i=to[pre[i]^1]) w[pre[i]]-=flow,w[pre[i]^1]+=flow;
		ans+=dis[T]*flow;
}
int main()
{
		//freopen("1.in","r",stdin);
		scanf("%d%d",&n,&m);
		cnt=n,S=++cnt,T=++cnt;
		for(int i=1,x,y,c,f;i<=m;i++)
		{
				scanf("%d%d%d%d",&x,&y,&c,&f);
				link(x,y,f,f,0);
				link(x,y,0,INF,2);
				link(y,x,0,min(c,f),1);
				if(c>=f) link(x,y,0,c-f,1);
				else
				{
						link(y,x,0,f-c,0);
						ans+=f-c;
				}
		}
		link(n,1,0,INF,0);
		for(int i=1;i<=n;i++)
		{
				if(de[i]>0) link(S,i,0,de[i],0);
				else link(i,T,0,-de[i],0);
		}
		while(Spfa()) Ek();
		printf("%d\n",ans);
		return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct edge
{
	int v,nxt,flow,w;
}e[500050];

int head[205],ecnt;
long long dep[205];
bool inq[205];
int n,m;
int s,t;
const long long inf = 1e18;

void adde(int u,int v,int fl,int w)
{
	e[ecnt].v = v;
	e[ecnt].nxt = head[u];
	e[ecnt].flow = fl;
	e[ecnt].w = w;
	head[u] = ecnt ++;
}

void Adde(int u,int v,int fl,int w)
{
	adde(u,v,fl,w);
	adde(v,u,0,-w);
}

bool spfa()
{
    deque<long long> que;
   	for(int i = 0;i <= t; ++ i)
   		dep[i] = (1ll << 62);
    memset(inq,0,sizeof(inq));
    dep[t] = 0;
    que.push_back(t);
    while(!que.empty())
    {
        long long u = que.front();
        que.pop_front();
        for(long long i = head[u];~i;i = e[i].nxt)
        {
            long long v = e[i].v;
            if(e[i ^ 1].flow && dep[u] - e[i].w < dep[v])
            {
                dep[v] = dep[u] + e[i ^ 1].w;
                if(!inq[v])
                {
                    if(que.empty() || dep[v] < dep[que.front()])
                        que.push_front(v);
                    else
                        que.push_back(v);
                    inq[v] = true;
                }
            }
        }
        inq[u] = false;
    }
    return dep[s] < (1ll << 62);
}

int curedge[205];
long long ans;
bool vis[205];

long long dfs(long long s,long long t,long long flow)
{
    if(s == t)
        return flow;
    long long delta = flow;
    vis[s] = true;
    for(long long i = head[s];~i;i = e[i].nxt)
    {
        long long v = e[i].v;
        if(!vis[v] && dep[v] == dep[s] - e[i].w && e[i].flow)
        {
            //prlong longf("%d %d\n",s,v);
            long long g = dfs(v,t,min((long long)e[i].flow,delta));
            delta -= g;
            ans += g * e[i].w;
            e[i].flow -= g;
            e[i ^ 1].flow += g;
            if(!delta)
                return flow;
        }
    }
    return flow - delta;
}

long long dinic()
{
    long long anss = 0;
    while(spfa())
    {
    	vis[t] = 1;
    	while(vis[t])
    	{
        	memset(vis,0,sizeof(vis));
        	anss += dfs(s,t,inf);
        }
    }
    return anss;
}

int pre[205];
int fs[205];

int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	s = 0,t = n + 1;
	int cos = 0;
	for(int i = 1;i <= m; ++ i)
	{
		int u,v,c,f; scanf("%d%d%d%d",&u,&v,&c,&f);
	//	Adde(u,v,f,0);
		fs[u] -= f; fs[v] += f;
		if(c > f)
		{
			Adde(v,u,f,1);
			Adde(u,v,c - f,1);
			Adde(u,v,0x3f3f3f3f,2);
		}
		else
		{
			cos += f - c;
			Adde(u,v,0x3f3f3f3f,2);
			Adde(v,u,f - c,0);
			Adde(v,u,c,1);
		}
	}
	for(int i = 1;i <= n; ++ i)
		if(fs[i] > 0)
			Adde(s,i,fs[i],0);
		else
			Adde(i,t,-fs[i],0);
	Adde(n,1,0x3f3f3f3f,0);
//	Adde(s,1,0x3f3f3f3f,0); Adde(n,t,0x3f3f3f3f,0);
	dinic();
	printf("%lld\n",ans + cos);
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
#define outval(x) cerr<<#x" = "<<x<<endl
#define outtag(x) cerr<<"-----------------"#x"-----------------\n"
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
                    For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int,int> pii;
LL read(){
    LL x=0,f=0;
    char ch=getchar();
    while (!isdigit(ch))
        f=ch=='-',ch=getchar();
    while (isdigit(ch))
        x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return f?-x:x;
}
const int N=105,M=105,INF=1e9;
namespace flows{
	const int N=105,M=105*(1+3)*2,INF=1e9;
	int n,S,T;
	int flow,cost;
	struct edge{
		int x,y,cap,cost,nxt;
		edge(){}
		edge(int _x,int _y,int _cap,int _cost,int _nxt){
			x=_x,y=_y,cap=_cap,cost=_cost,nxt=_nxt;
		}
	}e[M];
	int fst[N],ec;
	void init(int _n,int _S,int _T){
		clr(fst),ec=1;
		flow=cost=0;
		n=_n,S=_S,T=_T;
	}
	void add(int x,int y,int cap,int cost){
//		printf("add(%d,%d,%d,%d)\n",x,y,cap,cost);
		e[++ec]=edge(x,y,cap,cost,fst[x]),fst[x]=ec;
		e[++ec]=edge(y,x,0,-cost,fst[y]),fst[y]=ec;
	}
	int dis[N],vis[N];
	bool spfa(){
		static int q[N];
		int head=0,tail=0;
		For(i,1,n)
			dis[i]=INF;
		clr(vis);
		q[++tail]=S,vis[S]=1,dis[S]=0;
		while (head<tail){
			int x=q[++head];
			for (int i=fst[x];i;i=e[i].nxt)
				if (e[i].cap){
					int y=e[i].y;
					if (dis[y]>dis[x]+e[i].cost){
						dis[y]=dis[x]+e[i].cost;
						if (!vis[y])
							q[++tail]=y,vis[y]=1;
					}
				}
		}
		clr(vis);
		return dis[T]<INF;
	}
	int dfs(int x,int lim){
		if (x==T||!lim)
			return lim;
		vis[x]=1;
		int rem=lim;
		for (int i=fst[x];i;i=e[i].nxt){
			int y=e[i].y;
			if (!vis[y]&&e[i].cap&&dis[x]+e[i].cost==dis[y]){
				int d=dfs(y,min(rem,e[i].cap));
				rem-=d;
				e[i].cap-=d;
				e[i^1].cap-=d;
				if (!rem)
					break;
			}
		}
		vis[x]=0;
		return lim-rem;
	}
	void Solve(){
		while (spfa()){
			int d=dfs(S,INF);
			flow+=d;
			cost+=d*dis[T];
//			outval(d),outval(dis[S]);
		}
	}
}
int n,m;
int in[N];
int ans=0;
int main(){
	n=read(),m=read();
	int S=n+1,T=S+1;
	flows::init(T,S,T);
	For(i,1,m){
		int x=read(),y=read(),cost=read(),flow=read();
//		if (x==1&&y==n){
//			ans+=max(0,flow-cost);
//			continue;
//		}
		in[x]-=flow;
		in[y]+=flow;
		if (flow<=cost){
			flows::add(y,x,flow,1);
			flows::add(x,y,cost-flow,1);
			flows::add(x,y,INF,2);
		}
		else {
			ans+=flow-cost;
			flows::add(y,x,flow-cost,0);
			flows::add(y,x,cost,1);
			flows::add(x,y,INF,2);
		}
	}
	flows::add(n,1,INF,0);
	For(i,1,n){
		if (in[i]>0){
			flows::add(S,i,in[i],0);
		}
		else if (in[i]<0){
			flows::add(i,T,-in[i],0);
		}
	}
	flows::Solve();
	ans+=flows::cost;
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define cri const register int
#define re register
using namespace std;
int fa[110],to[20010],la[20010],fl[20010],da[20010],du[110],cnt=1;
const int inf=1e9;
int dis[110],v[110],pre[110],que[20010],S,T;
inline void add(cri x,cri y,cri l,cri r,cri k){
	to[++cnt]=y,la[cnt]=fa[x],fa[x]=cnt,fl[cnt]=r-l,da[cnt]=k;
	to[++cnt]=x,la[cnt]=fa[y],fa[y]=cnt,fl[cnt]=0,da[cnt]=-k;
}
inline char spfa(int l=1,int r=0){
	for(int i=0;i<=T;i++) dis[i]=inf;
	que[++r]=S;dis[S]=0;
	while(l<=r){
		cri x=que[l++];v[x]=0;
		for(int i=fa[x];i;i=la[i]) if(fl[i]&&dis[to[i]]>dis[x]+da[i]){
			dis[to[i]]=dis[x]+da[i];pre[to[i]]=i;
			if(!v[to[i]]) que[++r]=to[i];
		}
	}
	return dis[T]!=dis[0];
}
inline int dfs(){
	int mn=inf;
	for(int i=pre[T];i;i=pre[to[i^1]]) mn=min(mn,fl[i]);
	for(int i=pre[T];i;i=pre[to[i^1]]) fl[i]-=mn,fl[i^1]+=mn;
	return mn;
}
inline int read(re int a=0,re char ch=getchar()){
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') a=(a<<3)+(a<<1)+(ch^48),ch=getchar();
	return a;
}
int main(){
	cri n=read(),m=read();
	int ans=0;S=n+1;T=n+2;
	for(int i=1;i<=m;i++){
		cri x=read(),y=read(),c=read(),f=read();
		du[x]-=f;du[y]+=f;
		if(c>=f){
			add(y,x,0,f,1);
			if(c>f) add(x,y,0,c-f,1);
			add(x,y,0,inf,2);
		}
		else {
			ans+=f-c;
			add(y,x,0,f-c,0);
			add(y,x,0,c,1);
			add(x,y,0,inf,2);
		}
	}
	add(n,1,0,inf,0);
	for(int i=1;i<=n;i++)
		if(du[i]>0) add(S,i,0,du[i],0);
		else if(du[i]<0) add(i,T,0,-du[i],0);
	while(spfa()) ans+=dis[T]*dfs();
	cout<<ans<<endl;
}


#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
#define mset(x, a) memset(x, a, sizeof(x))
using namespace std;
inline int read() {
	char ch = getchar(); int x = 0;
	while(ch < '0' || ch > '9') ch = getchar();
	while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
	return x;
}

const int maxn = 505, inf = 1e9;
int N, M, S, T, flow, cost, dis[maxn], iter[maxn], d[maxn];
struct Edge { int to, rev, cap, cost; };
vector <Edge> G[maxn];
bool vis[maxn];

void AddEdge(int x, int y, int cap, int cost) {
    G[x].push_back((Edge){ y, G[y].size(), cap, cost });
    G[y].push_back((Edge){ x, G[x].size()-1, 0, -cost });
}

bool SPFA() {
    queue <int> q;
    for(int i = S; i <= T; ++i) iter[i] = vis[i] = 0, dis[i] = inf;
    q.push(T); dis[T] = 0; vis[T] = true;
    while(!q.empty()) {
        int v = q.front(); q.pop(); vis[v] = false;
        for(int i = 0; i < G[v].size(); ++i) {
            Edge &e = G[v][i];
            if(G[e.to][e.rev].cap && dis[e.to] > dis[v]-e.cost) {
                dis[e.to] = dis[v]-e.cost;
                if(!vis[e.to]) vis[e.to] = true, q.push(e.to);
            }
        }
    }
    mset(vis, 0);
    return dis[S] < inf;
}

int dfs(int v, int f) {
    vis[v] = true;
    if(v == T || !f) return f;
    for(int &i = iter[v]; i < G[v].size(); ++i) {
        Edge &e = G[v][i];
        if(e.cap && !vis[e.to] && dis[v]-e.cost == dis[e.to]) {
            int d = dfs(e.to, min(e.cap, f));
            if(d > 0)
            { cost += d*e.cost, e.cap -= d, G[e.to][e.rev].cap += d; return d; }
        }
    }
    return 0;
}

void ZKW() {
    while(SPFA())
        for(int f; (f = dfs(S, inf)) > 0; ) flow += f;
}

int main() {
	scanf("%d%d", &N, &M);
	S = 0, T = N+1;
	for(int i = 1, u, v, c, f; i <= M; ++i) {
		u = read(), v = read(), c = read(), f = read();
		d[u] -= f;
		d[v] += f;
		if(f > c) {
			cost += f-c;
			AddEdge(v, u, f-c, 0);
			AddEdge(v, u, c, 1);
			AddEdge(u, v, inf, 2);
		}
		else {
			AddEdge(v, u, f, 1);
			AddEdge(u, v, c-f, 1);
			AddEdge(u, v, inf, 2);
		}
	}
	AddEdge(N, 1, inf, 0);
	for(int i = 1; i <= N; ++i)
		if(d[i] < 0) AddEdge(i, T, -d[i], 0);
		else AddEdge(S, i, d[i], 0);

	ZKW();
	printf("%d\n", cost);
	return 0;
}


#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<=tot;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,0x3f3f3f3f,2);
		}
	}
	made(n,1,0x3f3f3f3f,0);
	MCMF();
	cout<<_+mincost;
	return 0;
}


#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,1);
			made(u,v,0x3f3f3f3f,2);
		}
	}
	made(n,1,0x3f3f3f3f,0);
	MCMF();
	cout<<_+mincost;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef int T1;//for cost
typedef int T2;//for flow
const int maxn = 107;
const T1 INF = 1e9;
const T2 inf = 1e9;
const T1 eps = 0;
struct Edge {
    int from, to;
    T2 cap, flow;
    T1 cost;
};
struct MCMF {//0-indexed
    int n, m, s, t;
    vector<Edge> edges;
    vector<int> G[maxn];
    int p[maxn],inq[maxn];
    T1 d[maxn];
    T2 a[maxn];
    void init(int n) {
        this->n = n;
        for(int i = 0; i < n; i++) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,T2 cap,T1 cost) {
        //cout << from << " - " << to << " - " << cap << " - " << cost << endl;
        edges.push_back((Edge){from, to, cap, 0, cost});
        edges.push_back((Edge){to, from, 0, 0, -cost});
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    pair<T1,T2> Mincost(int s,int t) {//bellmanFord
        T1 tot_cost = 0;
        T2 tot_flow = 0;
        while(true) {
            for(int i = 0; i < n; i++) d[i] = INF;
            d[s] = 0;
            p[s] = 0;
            a[s] = inf;
            bool up=true;
            while(up) {
                up=false;
                for(int u = 0; u < n; u++) {
                    if(d[u]-INF>=-eps)continue;
                    for(int j:G[u]) {
                        Edge &e=edges[j];
                        if(e.cap > e.flow && d[e.to] > d[u] + e.cost+eps) {
                            d[e.to] = d[u] + e.cost;
                            p[e.to] = j;
                            a[e.to] = min(a[u], e.cap - e.flow);
                            up=true;
                        }
                    }
                }
            }
            if(abs(d[t]-INF)<=eps)break;
            tot_cost += (T1)d[t] * a[t];
            tot_flow += (T2)a[t];
            int u = t;
            while(u != s) {
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
                u = edges[p[u]].from;
            }
        }
        return {tot_cost,tot_flow};
    }
    pair<T1,T2> Mincost2(int s,int t) {//SPFA
        T1 tot_cost = 0;
        T2 tot_flow = 0;
        while(true) {
            for(int i = 0; i < n; i++) d[i] = INF;
            memset(inq, 0, sizeof(inq));
            d[s] = 0;
            inq[s] = 1;
            p[s] = 0;
            a[s] = inf;
            queue<int> Q;
            srand(time(NULL));
            Q.push(s);
            while(!Q.empty()) {
                int u = Q.front();
                Q.pop();
                inq[u] = 0;
                for(int i = 0; i < G[u].size(); i++) {
                    Edge& e = edges[G[u][i]];
                    if(e.cap > e.flow && d[e.to] > d[u] + e.cost+eps) {
                        d[e.to] = d[u] + e.cost;
                        p[e.to] = G[u][i];
                        a[e.to] = min(a[u], e.cap - e.flow);
                        if(!inq[e.to]) {
                            Q.push(e.to);
                            inq[e.to] = 1;
                        }
                    }
                }
            }
            if(abs(d[t]-INF)<=eps)break;
            tot_cost += (T1)d[t] * a[t];
            tot_flow += a[t];
            int u = t;
            while(u != s) {
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
                u = edges[p[u]].from;
            }
        }

        return {tot_cost,tot_flow};
    }
} mcmf;

int n , m ;

int flow[105];

int main(){
    cin>>n>>m;
    int s = 0 ;
    int t = n+1;
    mcmf.init(n+2);
    int ans = 0 ;

    for(int i=0;i<m;i++){
        int u,v,c,f;
        cin >> u >> v >> c >> f ;

        flow[u] += f ;
        flow[v] -= f ;

        if(c<f) ans += f - c ;
        if(f<=c){
            mcmf.AddEdge(u,v,inf,2);
            mcmf.AddEdge(u,v,c-f,1);
            mcmf.AddEdge(v,u,f,1);
        }
        else{
            mcmf.AddEdge(u,v,inf,2);
            mcmf.AddEdge(v,u,f-c,0);
            mcmf.AddEdge(v,u,c,1);
        }

    }

    for(int i=1;i<=n;i++){
        if(flow[i]<0) mcmf.AddEdge(s,i,-flow[i],0) ;
        else          mcmf.AddEdge(i,t, flow[i],0);
    }

    mcmf.AddEdge(n,1,inf,0);

    ans = ans + mcmf.Mincost(s,t).first ;

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef int T1;//for cost
typedef int T2;//for flow
const int maxn = 107;
const T1 INF = 1e9;
const T2 inf = 1e9;
const T1 eps = 0;
struct Edge {
    int from, to;
    T2 cap, flow;
    T1 cost;
};
struct MCMF {//0-indexed
    int n, m, s, t;
    vector<Edge> edges;
    vector<int> G[maxn];
    int p[maxn],inq[maxn];
    T1 d[maxn];
    T2 a[maxn];
    void init(int n) {
        this->n = n;
        for(int i = 0; i < n; i++) G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,T2 cap,T1 cost) {
        //cout << from << " - " << to << " - " << cap << " - " << cost << endl;
        edges.push_back((Edge){from, to, cap, 0, cost});
        edges.push_back((Edge){to, from, 0, 0, -cost});
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    pair<T1,T2> Mincost(int s,int t) {//bellmanFord
        T1 tot_cost = 0;
        T2 tot_flow = 0;
        while(true) {
            for(int i = 0; i < n; i++) d[i] = INF;
            d[s] = 0;
            p[s] = 0;
            a[s] = inf;
            bool up=true;
            while(up) {
                up=false;
                for(int u = 0; u < n; u++) {
                    if(d[u]-INF>=-eps)continue;
                    for(int j:G[u]) {
                        Edge &e=edges[j];
                        if(e.cap > e.flow && d[e.to] > d[u] + e.cost+eps) {
                            d[e.to] = d[u] + e.cost;
                            p[e.to] = j;
                            a[e.to] = min(a[u], e.cap - e.flow);
                            up=true;
                        }
                    }
                }
            }
            if(abs(d[t]-INF)<=eps)break;
            tot_cost += (T1)d[t] * a[t];
            tot_flow += (T2)a[t];
            int u = t;
            while(u != s) {
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
                u = edges[p[u]].from;
            }
        }
        return {tot_cost,tot_flow};
    }
    pair<T1,T2> Mincost2(int s,int t) {//SPFA
        T1 tot_cost = 0;
        T2 tot_flow = 0;
        while(true) {
            for(int i = 0; i < n; i++) d[i] = INF;
            memset(inq, 0, sizeof(inq));
            d[s] = 0;
            inq[s] = 1;
            p[s] = 0;
            a[s] = inf;
            queue<int> Q;
            srand(time(NULL));
            Q.push(s);
            while(!Q.empty()) {
                int u = Q.front();
                Q.pop();
                inq[u] = 0;
                for(int i = 0; i < G[u].size(); i++) {
                    Edge& e = edges[G[u][i]];
                    if(e.cap > e.flow && d[e.to] > d[u] + e.cost+eps) {
                        d[e.to] = d[u] + e.cost;
                        p[e.to] = G[u][i];
                        a[e.to] = min(a[u], e.cap - e.flow);
                        if(!inq[e.to]) {
                            Q.push(e.to);
                            inq[e.to] = 1;
                        }
                    }
                }
            }
            if(abs(d[t]-INF)<=eps)break;
            tot_cost += (T1)d[t] * a[t];
            tot_flow += a[t];
            int u = t;
            while(u != s) {
                edges[p[u]].flow += a[t];
                edges[p[u]^1].flow -= a[t];
                u = edges[p[u]].from;
            }
        }

        return {tot_cost,tot_flow};
    }
} mcmf;

int n , m ;

int flow[105];

int main(){
    cin>>n>>m;
    int s = 0 ;
    int t = n+1;
    mcmf.init(n+2);
    int ans = 0 ;

    for(int i=0;i<m;i++){
        int u,v,c,f;
        cin >> u >> v >> c >> f ;

        flow[u] += f ;
        flow[v] -= f ;

        if(c<f) ans += f - c ;
        if(f<=c){
            mcmf.AddEdge(u,v,inf,2);
            mcmf.AddEdge(u,v,c-f,1);
            mcmf.AddEdge(v,u,f,1);
        }
        else{
            mcmf.AddEdge(u,v,inf,2);
            mcmf.AddEdge(v,u,f-c,0);
            mcmf.AddEdge(v,u,c,1);
        }

    }

    for(int i=1;i<=n;i++){
        if(flow[i]<0) mcmf.AddEdge(s,i,-flow[i],0) ;
        else          mcmf.AddEdge(i,t, flow[i],0);
    }

    mcmf.AddEdge(n,1,inf,0);

    ans = ans + mcmf.Mincost2(s,t).first ;

    cout << ans << 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(1, n + n, 0, inf / 10);
	F.insert(n, 1 + n, 0, inf / 10);
	FOE(i, 1, n){
		F.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);
			c = d;
		} else {
			F.insert(a, b + n, 1, d - c);
		}
		F.insert(b, a + n, 1, c);
		F.insert(a, b + n, 2, inf / 10);
		nat[a] -= c; nat[b] += c;
	}

	FOE(i, 1, n) if (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	F.CostedMaxFlow(S, T);
	cout << 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 (nat[i] > 0) F.insert(S, i, 0, nat[i]);
	else F.insert(i + n, T, 0, -nat[i]);
	FOE(i, 1, 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;
}