class UF: def __init__(self,n): self.p = [-1]*n def f(self,x): if self.p[x]<0: return x else: self.p[x] = self.f(self.p[x]); return self.p[x] def u(self,x,y): x = self.f(x); y = self.f(y) if x==y: return if -self.p[x]<-self.p[y]: x,y = y,x self.p[x] += self.p[y]; self.p[y] = x def s(self,x,y): return self.f(x) == self.f(y) n,k,c = map(int,input().split()) wpuv = [] for _ in range(k): u,v,w,p = map(int,input().split()) wpuv.append((w,p,u-1,v-1)) uf = UF(n); adj = [[] for _ in range(n)]; d = m = 0 for w,p,u,v in sorted(wpuv): if not uf.s(u,v): uf.u(u,v); d += w; m = max(m,p) adj[u-1].append(w); adj[v-1].append(w) e = [max(adj[i]) for i in range(n)] if d>c: exit(print(-1)) for w,p,u,v in wpuv: if d-max(e[u],e[v])+w<=c: m = max(m,p) print(m)