import heapq import sys rr = sys.stdin N,M,X = map(int,rr.readline().split()) BB = 0 G = [[] for _ in range(N)] for _ in range(M): u,v,a,b = map(int,rr.readline().split()) u -= 1 v -= 1 if BB < b:BB = b G[u].append((b,a,v)) G[v].append((b,a,u)) for i in range(N): G[i].sort(reverse = True) X = min(X,10 ** 14) inf = 1 << 50 B = 47 mask = (1 << B) - 1 def calc(k): dist = [inf] * N dist[0] = 0 q = [0] while q: U = heapq.heappop(q) d = U >> B now = U & mask if dist[now] > d:continue if now == N - 1:break for b,a,v in G[now]: if b < k:break if dist[v] > d + a: dist[v] = d + a heapq.heappush(q,((d + a) << B) + v) return dist[-1] <= X end = BB + 1 start = 0 while end - start > 1: mid = end + start >> 1 k = mid dist = [inf] * N dist[0] = 0 q = [0] while q: U = heapq.heappop(q) d = U >> B now = U & mask if dist[now] > d:continue if now == N - 1:break for b,a,v in G[now]: if b < k:continue if dist[v] > d + a: dist[v] = d + a heapq.heappush(q,((d + a) << B) + v) if dist[-1] <= X: start = mid else: end = mid """ if calc(mid): start = mid else: end = mid """ print(start if start != 0 else -1)