import sys input = sys.stdin.readline from heapq import heappop,heappush N,M,X=map(int,input().split()) E=[[] for i in range(N+1)] for i in range(M): x,y,c,t=map(int,input().split()) E[x].append((y,t,c)) E[y].append((x,t,c)) DIS=[(1<<60,1<<60)]*(N+1) DIS[1]=(0,0) Q=[(0,0,1)] while Q: x,y,town=heappop(Q) if DIS[town]!=(x,y): continue for to,time,cost in E[town]: z=x+time w=y+cost z+=w//X w%=X if DIS[to]>(z,w): DIS[to]=(z,w) heappush(Q,(z,w,to)) #print(DIS[N]) if DIS[N][0]>=1<<58: print(-1) else: ANS=DIS[N][0] if DIS[N][1]>0: ANS+=1 print(ANS)