#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define INFLL 0x3f3f3f3f3f3f3f3f
#define N 510
#define M 100010
struct Dicnic {
struct Edge {
int to, nxt;
db flow;
Edge() {}
Edge(int to, int nxt, db flow) : to(to), nxt(nxt), flow(flow) {}
} edge[M << 1];
int head[N], dep[N];
int S, T, tot;
void Init() {
memset(head, -1, sizeof head);
tot = 0;
}
void addedge(int u, int v, db w, db rw = 0) {
edge[tot] = Edge(v, head[u], w);
head[u] = tot++;
edge[tot] = Edge(u, head[v], rw);
head[v] = tot++;
}
bool BFS() {
memset(dep, -1, sizeof dep);
queue<int> q;
q.push(S);
dep[S] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (edge[i].flow && dep[edge[i].to] == -1) {
dep[edge[i].to] = dep[u] + 1;
q.push(edge[i].to);
}
}
}
return dep[T] >= 0;
}
db DFS(int u, db f) {
if (u == T || f == 0) return f;
db w, used = 0;
for (int i = head[u]; ~i; i = edge[i].nxt) {
if (edge[i].flow > 0 && dep[edge[i].to] == dep[u] + 1) {
w = DFS(edge[i].to, min(f - used, edge[i].flow));
edge[i].flow -= w;
edge[i ^ 1].flow += w;
used += w;
if (used == f) return f;
}
}
if (!used) dep[u] = -1;
return used;
}
db solve() {
db ans = 0;
while (BFS()) {
ans += DFS(S, INFLL);
}
return ans;
}
} dicnic;
int n, m;
int u, v, a, b, c;
ll w1[N], w2[N];
int main() {
while (scanf("%d %d", &n, &m) != EOF) {
memset(w1, 0, sizeof w1);
memset(w2, 0, sizeof w2);
dicnic.Init();
dicnic.S = 0, dicnic.T = n + 1;
ll ans = 0;
for (int i = 1; i <= m; ++i) {
scanf("%d %d %d %d %d", &u, &v, &a, &b, &c);
w1[u] += a + b;
w1[v] += a + b;
w2[u] += b + c;
w2[v] += b + c;
dicnic.addedge(u, v, -b * 1.0 + a / 2.0 + c / 2.0);
dicnic.addedge(v, u, -b * 1.0 + a / 2.0 + c / 2.0);
ans += a + b + c;
}
for (int i = 1; i <= n; ++i) {
dicnic.addedge(dicnic.S, i, w1[i] / 2.0);
dicnic.addedge(i, dicnic.T, w2[i] / 2.0);
}
ans = (ll) round(ans - dicnic.solve());
printf("%lld\n", ans);
}
return 0;
}