from heapq import heapify, heappop, heappush N, M, X = map(int, input().split()) G = [[] for _ in range(N)] for _ in range(M): u, v, C, T = map(int, input().split()) u -= 1; v -= 1 G[u].append((v, C, T)) G[v].append((u, C, T)) H = [(0, 0, 0)] heapify(H) vis = [False] * N vis[0] = True Time = [10 ** 9] * N Time[0] = 0 Cost = [0] * N while len(H) > 0: T, u, C = heappop(H) for v, C1, T1 in G[u]: if vis[v]: continue if Time[v] > Time[u] + T1: Time[v] = Time[u] + T1 Cost[v] = Cost[u] + C1 heappush(H, (Time[v], v, Cost[v])) if Time[N - 1] == 10 ** 9: print(-1) else: print(Time[N - 1] + (Cost[N - 1] + X - 1) // X)