import heapq def min_time_to_reach(N, M, X, roads): graph = [[] for _ in range(N + 1)] for u, v, C, T in roads: work_time = C // X + (1 if C % X != 0 else 0) graph[u].append((v, work_time + T)) graph[v].append((u, work_time + T)) distances = [float('inf')] * (N + 1) distances[1] = 0 queue = [(0, 1)] while queue: total_time, u = heapq.heappop(queue) if total_time > distances[u]: continue for v, time in graph[u]: if distances[u] + time < distances[v]: distances[v] = distances[u] + time heapq.heappush(queue, (distances[v], v)) return distances[N] if distances[N] != float('inf') else -1 N, M, X = map(int, input().split()) roads = [tuple(map(int, input().split())) for _ in range(M)] min_time = min_time_to_reach(N, M, X, roads) print(min_time - 1 if min_time != -1 else -1)