import sys, math sys.setrecursionlimit(10**6) INF = float('inf') mod = 10**9 + 7 #mod = 998244353 input = lambda: sys.stdin.readline().rstrip() def li(): return list(map(int, input().split())) from collections import deque, defaultdict class Dinic: def __init__(self, n): self.n = n self.links = [[] for _ in range(n)] self.depth = None self.progress = None def add_link(self, _from, to, cap): self.links[_from].append([cap, to, len(self.links[to])]) self.links[to].append([0, _from, len(self.links[_from]) - 1]) def bfs(self, s): depth = [-1] * self.n depth[s] = 0 q = deque([s]) while q: v = q.popleft() for cap, to, rev in self.links[v]: if cap > 0 and depth[to] < 0: depth[to] = depth[v] + 1 q.append(to) self.depth = depth def dfs(self, v, t, flow): if v == t: return flow links_v = self.links[v] for i in range(self.progress[v], len(links_v)): self.progress[v] = i cap, to, rev = link = links_v[i] if cap == 0 or self.depth[v] >= self.depth[to]: continue d = self.dfs(to, t, min(flow, cap)) if d == 0: continue link[0] -= d self.links[to][rev][0] += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) if self.depth[t] < 0: return flow self.progress = [0] * self.n current_flow = self.dfs(s, t, INF) while current_flow > 0: flow += current_flow current_flow = self.dfs(s, t, INF) N, M, d = li() lists = [[] for _ in range(N+1)] inp = [] for _ in range(M): u, v, p, q, w = li() q += d lists[u].append(p) lists[v].append(q) inp.append((u, v, p, q, w)) dicts = [] acc = [0] for i in range(N+1): lists[i] = list(set(lists[i])) lists[i].sort() d = {i: e for e, i in enumerate(lists[i])} dicts.append(d) acc.append(acc[-1] + len(lists[i])) dinic = Dinic(acc[-1] + 2) for i in range(M): u, v, p, q, w = inp[i] idx1 = acc[u] + dicts[u][p] idx2 = acc[v] + dicts[v][q] dinic.add_link(idx1, idx2, w) t = acc[-1] + 1 for i in range(1, N+1): for j in range(len(dicts[i])-1): idx = acc[i] + j dinic.add_link(idx, idx+1, INF) print(dinic.max_flow(0, acc[-1]-1 ))