## https://yukicoder.me/problems/no/2387 import heapq def solve(N, next_node, X, width): fix = {} seen = {0:0} queue= [] heapq.heappush(queue, (0, 0)) while len(queue) > 0: cost, v = heapq.heappop(queue) if v in fix: continue fix[v] = cost for next_v, a, b in next_node[v]: if next_v in fix: continue if b < width: continue new_cost = cost + a if next_v not in seen or seen[next_v] > new_cost: seen[next_v] = new_cost heapq.heappush(queue, (new_cost, next_v)) if (N - 1) not in fix: return False else: return fix[N - 1] <= X def main(): N, M, X = map(int, input().split()) next_node = [[] for _ in range(N)] max_b = 0 for _ in range(M): u, v, a,b= map(int, input().split()) next_node[u - 1].append((v - 1, a, b)) next_node[v - 1].append((u - 1, a, b)) max_b = max(max_b, b) low = 0 high = max_b while high - low > 1: mid = (high + low) // 2 if solve(N, next_node, X, mid): low = mid else: high = mid if solve(N, next_node, X, high): v = high else: v = low if v == 0: print(-1) else: print(v) if __name__ == "__main__": main()