class UnionFind: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n alist=[] while self.parent_or_size[a] >= 0: alist.append(a) a = self.parent_or_size[a] for i in alist: self.parent_or_size[i] = a return a def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] N,K,C=map(int,input().split()) edge=[] edge_s=[] edge_w=[] for i in range(K): u,v,w,p=map(int,input().split()) u-=1 v-=1 edge.append((u,v,w,p)) edge_s.append((p,i)) edge_w.append((w,i)) edge_s.sort(key=lambda x:x[0]) edge_w.sort(key=lambda x:x[0]) for i in range(K-1,0,-1): e1=edge_s[i][1] e0=edge_s[i-1][1] if edge[e1][2]<=edge[e0][2]: edge_s[i-1]=edge_s[i] uf=UnionFind(N) nc=0 for _,i in edge_w: u,v,w,p=edge[i] if uf.same(u,v): continue uf.merge(u,v) nc+=w if nc>C: print(-1) exit() d=[0,K] while d[1]-d[0]>1: m=sum(d)//2 use=edge_s[m][1] uf=UnionFind(N) nc=0 u,v,w,p=edge[use] uf.merge(u,v) nc+=w for _,i in edge_w: u,v,w,p=edge[i] if uf.same(u,v): continue uf.merge(u,v) nc+=w if nc<=C: d[0]=m else: d[1]=m use=edge_s[d[0]][1] print(edge[use][3])