#include using namespace std; using ll = long long; using T = tuple; int main(){ ios::sync_with_stdio(false); cin.tie(0); ll C, c = 0; int N, K, ans = 0, i = 0; cin >> N >> K >> C; vector ps(N, -1), d(N, 1 << 30), mx(K); 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()); for(auto [w, v, u, p] : E){ while(u != v){ if(d[v] > d[u]) swap(v, u); if(ps[v] < 0){ N--; c += w; mx[i] = w; while(ps[u] >= u) u = ps[u]; if(-ps[v] < -ps[u]) swap(u, v); ps[v] += ps[u]; ps[u] = v; d[u] = w; break; } mx[i] = d[v], v = ps[v]; } i++; } if(c > C || N != 1){ cout << -1 << '\n'; exit(0); } i = 0; for(auto [w, v, u, p] : E) if(c + w - mx[i++] <= C) ans = max(ans, p); cout << ans << '\n'; }