from collections import defaultdict from heapq import heappush, heappop INF = 1 << 60 N, M, X = map(int, input().split()) adj = defaultdict(list) for _ in range(M): u, v, a, b = map(int, input().split()) u -= 1 v -= 1 adj[u].append((v, a, b)) adj[v].append((u, a, b)) def bsearch(low: int, high: int, fun, is_complement=False) -> int: def pred(x: int) -> bool: return not fun(x) if is_complement else fun(x) assert pred(low) lo = low hi = high res = low while lo <= hi: m = (lo + hi) // 2 if pred(m): res = max(res, m) lo = m + 1 else: hi = m - 1 return res + 1 if is_complement else res # 大きさ m を輸送できるか def can(m: int) -> bool: q = [(0, 0)] used = [INF] * N while q: tm, v = heappop(q) if v == N-1: return True if used[v] <= tm: continue used[v] = tm for to, a, b in adj[v]: if b < m: continue if tm + a > X: continue heappush(q, (tm + a, to)) return False if can(1): ans = bsearch(1, INF, can) print(ans) else: print(-1)