#include using namespace std; //#include //using namespace atcoder; using ll = long long int; using ull = unsigned long long int; using ld = long double; constexpr ll MAX = 2000000000000000000; constexpr ld PI = 3.14159265358979; constexpr ll MOD = 0;//2024948111; ld dotorad(ld K){ return PI * K / 180.0; } ld radtodo(ld K){ return K * 180.0 / PI; } mt19937 mt; void randinit(){ srand((unsigned)time(NULL));mt = mt19937(rand()); } int main(){ ll N,M,X; cin >> N >> M >> X; vector> G(N),C(N),T(N); for(ll i = 0;i < M;i++){ ll u,v,CC,TT; cin >> u >> v >> CC >> TT; u--;v--; G[u].emplace_back(v); C[u].emplace_back(CC); T[u].emplace_back(TT); G[v].emplace_back(u); C[v].emplace_back(CC); T[v].emplace_back(TT); } vector D(N,MAX); priority_queue,vector>,greater>> que; que.emplace(0,0,0); while(!que.empty()){ auto [d,nokori,a] = que.top(); que.pop(); if(D[a] == MAX){ D[a] = d; for(ll i = 0;i < (ll)G[a].size();i++){ ll b = G[a][i]; ll c = C[a][i]; ll t = T[a][i]; ll newd = d + t + c / X; c %= X; ll new_nokori = nokori - c; if(new_nokori < 0){ new_nokori += X; newd += 1; } que.emplace(newd,new_nokori,b); } } } if(D[N - 1] == MAX) cout << -1 << endl; else cout << D[N - 1] << endl; }