# Dinic's algorithm from: # https://tjkendev.github.io/procon-library/python/max_flow/dinic.html from collections import deque class Dinic: def __init__(self, N): self.N = N self.G = [[] for i in range(N)] def add_edge(self, fr, to, cap): forward = [to, cap, None] forward[2] = backward = [fr, 0, forward] self.G[fr].append(forward) self.G[to].append(backward) def add_multi_edge(self, v1, v2, cap1, cap2): edge1 = [v2, cap1, None] edge1[2] = edge2 = [v1, cap2, edge1] self.G[v1].append(edge1) self.G[v2].append(edge2) def bfs(self, s, t): self.level = level = [None]*self.N deq = deque([s]) level[s] = 0 G = self.G while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in G[v]: if cap and level[w] is None: level[w] = lv deq.append(w) return level[t] is not None def dfs(self, v, t, f): if v == t: return f level = self.level for e in self.it[v]: w, cap, rev = e if cap and level[v] < level[w]: d = self.dfs(w, t, min(f, cap)) if d: e[1] -= d rev[1] += d return d return 0 def flow(self, s, t): flow = 0 INF = 10**9 + 7 G = self.G while self.bfs(s, t): *self.it, = map(iter, self.G) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow ########################################## n, m, k = map(int, input().split()) edges = [list(map(int, input().split())) for _ in range(m)] edges.sort(key=lambda x: x[2]) y = [0] * m s, t = 0, n+1 ysum = ans = 0 for i, (a, b, c, d) in enumerate(edges): g = Dinic(n+2) cs = [0] * (n+1) for j, (a2, b2, _, _) in enumerate(edges): g.add_edge(s, a2, y[j]) g.add_edge(a2, b2, y[j]) for v in range(1, n+1): g.add_edge(v, t, k) g.add_edge(s, a, k*n) g.add_edge(s, b, k*n) f = g.flow(s, t) z = f - k - ysum y[i] = min(z, d) ysum += y[i] ans += c * y[i] if ysum < k*(n-1): ans = -1 print(ans)