from collections import defaultdict,Counter,deque from heapq import heappush,heappop def main(): N,M,X =map(int,input().split()) e = [[] for _ in range(N)] m = 0 for _ in range(M): u,v,a,b = map(int,input().split()) u -= 1 v -= 1 m = max(b,m) e[u].append([v,a,b]) e[v].append([u,a,b]) ok = 0 ng = m+1 while ng - ok > 1: mid = (ok + ng)//2 v = [1 << 60] * N v[0] = 0 q = [] heappush(q,(0,0)) while q: t,p = heappop(q) for next,a,b in e[p]: if mid <= b and t + a < X and t + a < v[next]: v[next] = t + a heappush(q,(v[next],next)) if v[-1] <= X: ok = mid else: ng = mid if ok == 0: print(-1) else: print(ok) if __name__ == '__main__': main()