import sys import numpy as np from scipy.optimize import linprog N, M, K = map(int, input().split()) A = [-1] * M B = [-1] * M C = [-1] * M D = [-1] * M c2eid = [] for eid in range(M): A[eid], B[eid], C[eid], D[eid] = map(int, input().split()) A[eid] -= 1 B[eid] -= 1 c2eid.append((C[eid], eid)) c2eid.sort() class DSU(): def __init__(self, N: int): self.par = list(range(N)) self.sz = [1] * N def find(self, x: int): if self.par[x] != x: self.par[x] = self.find(self.par[x]) return self.par[x] # return x if self.par[x] == x else (self.par[x] = find(self, self.par[x])) def unite(self, x: int, y: int): x, y = self.find(x), self.find(y) if x == y: return False if self.sz[x] < self.sz[y]: x, y = y, x self.par[y] = x self.sz[x] += self.sz[y] return True dsu = DSU(N) ret = 0 mstedge = [0] * M tree = [[] for _ in range(N)] vpar = [-1] * N epar = [-1] * N depth = [0] * N for cost, eid in c2eid: u, v = A[eid], B[eid] if dsu.unite(u, v): mstedge[eid] = 1 ret += C[eid] * K tree[u].append((v, eid)) tree[v].append((u, eid)) def dfs(now: int, prv: int, dnow: int) -> None: depth[now] = dnow for nxt, eid in tree[now]: if nxt != prv: vpar[nxt] = now epar[nxt] = eid dfs(nxt, now, dnow + 1) dfs(0, -1, 0) xbound = (0, None) vecc = [D[eid] - K * mstedge[eid] for eid in range(M)] matA = [] vecb = [] for eid, ismst in enumerate(mstedge): if ismst == 0: u, v = A[eid], B[eid] while depth[u] > depth[v]: z = [0] * M z[epar[u]] = 1 z[eid] = -1 matA.append(z) vecb.append(C[eid] - C[epar[u]]) u = vpar[u] while depth[v] > depth[u]: z = [0] * M z[epar[v]] = 1 z[eid] = -1 matA.append(z) vecb.append(C[eid] - C[epar[v]]) v = vpar[v] while u != v: z = [0] * M z[epar[u]] = 1 z[eid] = -1 matA.append(z) vecb.append(C[eid] - C[epar[u]]) u = vpar[u] z = [0] * M z[epar[v]] = 1 z[eid] = -1 matA.append(z) vecb.append(C[eid] - C[epar[v]]) v = vpar[v] if len(matA) == 0: print(ret) else: res = linprog(vecc, method='simplex', A_ub=matA, b_ub = vecb, bounds = [xbound] * M, options={'maxiter': 10000, 'tol': 1e-9}) if res.success: print(ret - round(res.fun)) else: print(-1)