import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random class lca(): def __init__(self,n,edge): self.n = n self.edge = edge self.dist = [10**20] * n self.k = math.floor(math.log2(n)) self.ansestor = [[-1 for i in range(n)] for j in range(self.k + 1)] self.mini = [[-10**18 for i in range(n)] for j in range(self.k + 1)] self.bfs() for i in range(self.k): for j in range(n): if self.ansestor[i][j] == -1: self.ansestor[i+1][j] = -1 else: self.ansestor[i+1][j] = self.ansestor[i][self.ansestor[i][j]] for i in range(self.k): for j in range(n): self.mini[i+1][j] = max(self.mini[i+1][j],self.mini[i][j],self.mini[i][self.ansestor[i][j]]) def bfs(self): d = collections.deque() d.append(0) mou = set() mou.add(0) self.dist[0] = 0 while d: now = d.popleft() for i,j in self.edge[now]: if i not in mou: self.ansestor[0][i] = now self.mini[0][i] = j d.append(i) mou.add(i) self.dist[i] = self.dist[now] + 1 def ans(self,u,a): for i in reversed(range(self.k + 1)): if u == -1: break if (a >> i) & 1: u = self.ansestor[i][u] return u def ans2(self,u,a): res = -10**18 for i in reversed(range(self.k + 1)): if u == -1: break if (a >> i) & 1: res = max(res,self.mini[i][u]) u = self.ansestor[i][u] return res def calc(self,s,t): if self.dist[s] > self.dist[t]: s = self.ans(s,self.dist[s] - self.dist[t]) if self.dist[s] < self.dist[t]: t = self.ans(t,-self.dist[s] + self.dist[t]) for i in reversed(range(self.k)): ns = self.ansestor[i][s] nt = self.ansestor[i][t] if ns != nt: s = ns t = nt x = (0 if self.ansestor[0][s] == -1 else s if s == t else self.ansestor[0][s]) return x class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = collections.defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items()) #sys.setrecursionlimit(10**9) #sys.set_int_max_str_digits(0) input = sys.stdin.readline n,k,c = map(int,input().split()) edge = [[] for i in range(n)] e = [] for i in range(k): u,v,w,p = map(int,input().split()) u-=1 v-=1 edge[u].append((v,w)) edge[v].append((u,w)) e.append((p,w,u,v)) uf = UnionFind(n) e.sort(key=lambda x:x[1]) ans = 0 temp = 0 ed = [[] for i in range(n)] for p,w,u,v in e: if uf.same(u,v) == False: ans += w uf.union(u,v) ed[u].append((v,w)) ed[v].append((u,w)) if ans > c: exit(print(-1)) l = lca(n,ed) for p,w,u,v in e: x = l.calc(u,v) if ans - max(l.ans2(u,l.dist[u]-l.dist[x]),l.ans2(v,l.dist[v]-l.dist[x])) + w <= c: temp = max(temp,p) print(temp)