#include #include using mint = atcoder::modint998244353; //using mint = atcoder::modint1000000007; using namespace atcoder; using namespace std; using ll = long long; using pi = pair; using pl = pair; using vi = vector; using vm = vector; using vvi = vector; using vl = vector; using vvl = vector; using vpi = vector>; using vpl = vector>; #define rep(i,n) for (ll i = 0; i < n; i++) #define replr(i,l,r) for (ll i = l; i < r; i++) set abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; const int inf =1e9; const ll infl = 4e18; template bool chmax(T &a, const T& b) {if (a < b) {a = b; return true;}return false;} template bool chmin(T &a, const T& b) {if (a > b) {a = b; return true;}return false;} template T max(vector &v) {T m = 0;for (auto x : v) chmax(m, x);return m;} template T min(vector &v) {T m = inf;for (auto x : v) chmin(m, x);return m;} template void print(vector &v){for(auto x : v){cout<> N >> M >> X; vector> G(N); rep(i,M){ int a,b,c,t; cin >> a >> b >> c >> t; a--; b--; G[a].push_back({b,c,t}); G[b].push_back({a,c,t}); } vector> dist(N,{infl,infl}); dist[0] = {0,0}; priority_queue,int>,vector,int>>,greater,int>>> pq; pq.push({{0,0},0}); while(!pq.empty()){ auto [d,v] = pq.top(); pq.pop(); if(dist[v] < d) continue; ll t=d.first,x=-d.second; for(auto e : G[v]){ ll nt = t + (max(e.cost-x,(ll)0)+X-1)/X+e.time; ll nx = x + (max(e.cost-x,(ll)0)+X-1)/X*X-e.cost; if(chmin(dist[e.to],{nt,-nx})){ pq.push({dist[e.to],e.to}); } } } if (dist[N-1].first == infl) cout << -1 << endl; else cout << dist[N-1].first << endl; }