import sys input = lambda: sys.stdin.readline()[:-1] from heapq import heappush, heappop from collections import defaultdict N, M, X = map(int, input().split()) INF = float('inf') g = [defaultdict(list) for _ in range(N)] def main(): for _ in range(M): u, v, a, b = map(int, input().split()) u -= 1; v -= 1 g[u][v].append((a, b)) g[v][u].append((a, b)) ok = 0 ng = 1 << 60 while ng - ok > 1: mid = (ok + ng) // 2 if dijkstra(mid): ok = mid else: ng = mid print(ok if 0 < ok <= X else -1) def dijkstra(s): hq = [(0, 0)] dist = [INF] * N dist[0] = 0 while hq: d, u = heappop(hq) if dist[u] < d: continue for v in g[u]: for a, b in g[u][v]: if b < s: continue if dist[u] + a < dist[v]: dist[v] = dist[u] + a heappush(hq, (dist[v], v)) return dist[N - 1] <= X main()