import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } int root(int[] uf, int u) { return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u])); } bool connect(int[] uf, int u, int v) { u = uf.root(u); v = uf.root(v); if (u == v) return false; if (uf[u] > uf[v]) swap(u, v); uf[u] += uf[v]; uf[v] = u; return true; } struct MinCut { int n; long[][] adj; int[] opt; this(int n_) { n = n_; adj = new long[][](n, n); } void ae(int u, int v, long c) { adj[u][v] += c; adj[v][u] += c; } long solve() { long ret = long.max; auto p = iota(n).array; auto b = new long[n]; opt = []; for (; --n; ) { foreach (u; 0 .. n + 1) b[p[u]] = 0; foreach (u; 0 .. n) { int vm = u + 1; foreach (v; u + 1 .. n + 1) { b[p[v]] += adj[p[u]][p[v]]; if (b[p[vm]] < b[p[v]]) vm = v; } swap(p[u + 1], p[vm]); } if (chmin(ret, b[p[n]])) { opt = p[n .. $]; } foreach (u; 0 .. n - 1) { adj[p[u]][p[n - 1]] += adj[p[u]][p[n]]; adj[p[n - 1]][p[u]] += adj[p[n]][p[u]]; } } return ret; } }; enum INF = 4 * 10L^^18; void main() { try { for (; ; ) { const N = readInt(); const M = readInt(); const K = readLong(); auto A = new int[M]; auto B = new int[M]; auto C = new long[M]; auto D = new long[M]; foreach (i; 0 .. M) { A[i] = readInt() - 1; B[i] = readInt() - 1; C[i] = readLong(); D[i] = readLong(); } auto mc = new MinCut(N); foreach (i; 0 .. M) { mc.ae(A[i], B[i], D[i]); } const res = mc.solve; debug { writeln("res = ", res); writeln("opt = ", mc.opt); } long ans = -1; if (res < K) { ans = -1; } else { ans = 0; auto cs = C.dup; auto uf = new int[N]; uf[] = -1; for (; ; ) { debug { writefln("ans = %s, cs = %s, uf = %s", ans, cs, uf); } long[] ccs; foreach (i; 0 .. M) { const a = uf.root(A[i]); const b = uf.root(B[i]); if (a != b) { ccs ~= cs[i]; } } ccs = ccs.sort.uniq.array; if (ccs.empty) { break; } auto uf1 = new int[N]; uf1[] = -1; auto adj = new long[][](N, N); foreach (i; 0 .. M) { const a = uf.root(A[i]); const b = uf.root(B[i]); if (a != b && cs[i] == ccs[0]) { uf1.connect(a, b); adj[a][b] += D[i]; adj[b][a] += D[i]; } } auto uss = new int[][N]; foreach (u; 0 .. N) { uss[uf1.root(u)] ~= u; } auto col = new bool[N]; foreach (r; 0 .. N) { const len = cast(int)(uss[r].length); if (len >= 2) { auto mc1 = new MinCut(len); foreach (x; 0 .. len) foreach (y; x + 1 .. len) { mc1.ae(x, y, adj[uss[r][x]][uss[r][y]]); } const res1 = mc1.solve; debug { writefln(" uss[r] = %s, res1 = %s, opt = %s", uss[r], res1, mc1.opt); } if (res1 < K) { foreach (x; mc1.opt) { col[uss[r][x]] = true; } } } } bool upd = false; foreach (i; 0 .. M) { const a = uf.root(A[i]); const b = uf.root(B[i]); if (a != b && cs[i] == ccs[0] && col[a] != col[b]) { upd = true; ans -= D[i] * (ccs[1] - ccs[0]); cs[i] = ccs[1]; } } if (!upd) { foreach (i; 0 .. M) { const a = uf.root(A[i]); const b = uf.root(B[i]); if (a != b && cs[i] == ccs[0]) { debug { writeln("connect ", a, " ", b, " at ", ccs[0]); } if (uf.connect(a, b)) { ans += K * ccs[0]; } } } } } } writeln(ans); } } catch (EOFException e) { } }