#include using namespace std; using ll = long long; struct dsu { vector parent_or_size, dat; public: dsu(int n) : parent_or_size(n, -1), dat(n, 1 << 30) {} bool merge(int u, int v, int w){ int x = leader(u), y = leader(v); if (x == y) return false; if (-parent_or_size[x] < -parent_or_size[y]) swap(x, y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; dat[y] = w; return true; } int leader(int v) { while(parent_or_size[v] >= 0) v = parent_or_size[v]; return v; } int max_edge(int u, int v){ int ans = 0; while(u != v){ if (dat[u] > dat[v]) swap(u, v); ans = dat[u], u = parent_or_size[u]; } return ans; } }; using T = tuple; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll C, c = 0; int N, K, ans = 0; cin >> N >> K >> C; vector E(K); for(auto &&[u, v, w, p] : E){ cin >> u >> v >> w >> p; u--, v--; swap(w, u); } sort(E.begin(), E.end()); dsu uf(N); for(auto &&[w, v, u, p] : E){ if(uf.merge(u, v, w)) c += w, N--; } if(c > C || N != 1){ cout << -1 << '\n'; return 0; } for(auto &&[w, v, u, p] : E){ if(c - uf.max_edge(u, v) + w <= C) ans = max(ans, p); } cout << ans << '\n'; }