import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random 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()) e = [] for i in range(k): u,v,w,p = map(int,input().split()) u-=1 v-=1 e.append((p,w,u,v)) uf = UnionFind(n) e.sort(key=lambda x:x[1]) ans = 0 for p,w,u,v in e: if uf.same(u,v) == False: ans += w uf.union(u,v) if ans > c: exit(print(-1)) e.sort() l,r = 0,k ans =0 for i in range(20): m = (l+r)//2 uf = UnionFind(n) temp = 0 uf.union(e[m][2],e[m][3]) temp += e[m][1] for j in range(m): p,w,u,v = e[j] if uf.same(u,v) == False: uf.union(u,v) temp += w if uf.group_count() == 1 and temp <= c: ans = max(ans,e[m][0]) if temp <= c: l = m else: r = m print(ans)