#include using namespace std; const int LOG = 17; struct unionfind{ vector p; unionfind(int N): p(N, -1){ } int root(int x){ if (p[x] == -1){ return x; } else { p[x] = root(p[x]); return p[x]; } } bool same(int x, int y){ return root(x) == root(y); } void unite(int x, int y){ x = root(x); y = root(y); if (x != y){ p[x] = y; } } }; int main(){ int N, K; long long C; cin >> N >> K >> C; vector> E(K); for (int i = 0; i < K; i++){ int u, v, w, p; cin >> u >> v >> w >> p; u--; v--; E[i] = make_tuple(w, u, v, p); } sort(E.begin(), E.end()); vector>> E2(N); unionfind UF(N); long long S = 0; for (int i = 0; i < K; i++){ int u = get<1>(E[i]); int v = get<2>(E[i]); if (!UF.same(u, v)){ int w = get<0>(E[i]); S += w; UF.unite(u, v); E2[u].push_back(make_pair(w, v)); E2[u].push_back(make_pair(w, u)); } } if (S > C){ cout << -1 << endl; } else { vector p(N, -1); vector pw(N, 0); vector d(N, 0); queue Q; Q.push(0); while (!Q.empty()){ int v = Q.front(); Q.pop(); for (pair P : E2[v]){ int w = P.second; if (w != p[v]){ p[w] = v; d[w] = d[v] + 1; pw[w] = P.first; Q.push(w); } } } vector> pp(LOG, vector(N, -1)); vector> bl(LOG, vector(N, 0)); for (int i = 0; i < N; i++){ pp[0][i] = p[i]; bl[0][i] = pw[i]; } for (int i = 0; i < LOG - 1; i++){ for (int j = 0; j < N; j++){ if (pp[i][j] != -1){ pp[i + 1][j] = pp[i][pp[i][j]]; bl[i + 1][j] = max(bl[i][j], bl[i][pp[i][j]]); } } } auto get_max = [&](int u, int v){ if (d[u] > d[v]){ swap(u, v); } int ans = 0; for (int i = 0; i < LOG; i++){ if (((d[v] - d[u]) >> i & 1) == 1){ ans = max(ans, bl[i][v]); v = pp[i][v]; } } if (u == v){ return ans; } for (int i = LOG - 1; i >= 0; i--){ if (pp[i][u] != pp[i][v]){ ans = max(ans, bl[i][u]); ans = max(ans, bl[i][v]); u = pp[i][u]; v = pp[i][v]; } } ans = max(ans, pw[u]); ans = max(ans, pw[v]); return ans; }; int ans = 0; for (int i = 0; i < K; i++){ int w = get<0>(E[i]); int u = get<1>(E[i]); int v = get<2>(E[i]); if (S - get_max(u, v) + w <= C){ ans = max(ans, get<3>(E[i])); } } cout << ans << endl; } }