#pragma region my_template #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using pi = pair; using pl = pair; using ti = tuple; using tl = tuple; using vi = vector; using vpi = vector; using vti = vector; using vvi = vector; using usi = unordered_set; using vl = vector; using vpl = vector; using vtl = vector; using vvl = vector; using usl = unordered_set; #define range(i, l, r) for(int i = (int)(l); i < (int)(r); i++) #define rrange(i, l, r) for(int i = (int)(r)-1; i >= (int)(l); i--) #define rep(i, n) range(i, 0, n) #define rrep(i, n) rrange(i, 0, n) #define len(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define sum(a) accumulate(all(a), 0) #define it2idx(a, it) distance(a.begin(), it) #define idx2it(a, idx) next(a.begin(), idx) #define elif else if namespace io { #ifdef LOCAL ofstream dout("./dmp.txt"); #else ofstream dout("/dev/null"); #endif template istream &operator >> (istream &in, pair &a){ in >> a.first >> a.second; return in; } template istream &operator >> (istream &in, vector &a){ for(T &x: a) in >> x; return in; } template ostream &operator << (ostream &out, const pair &a){ out << a.first << " " << a.second; return out; } template ostream &operator << (ostream &out, const vector &a) { rep(i, len(a)) out << a[i] << (i == len(a)-1 ? "" : " "); return out; } template ostream &operator << (ostream &out, const unordered_set &a) { int i = 0; for (const T &x: a) { out << x << (i == len(a)-1 ? "" : " "); i++; } return out; } template ostream &operator << (ostream &out, const set &a) { int i = 0; for (const T &x: a) { out << x << (i == len(a)-1 ? "" : " "); i++; } return out; } template ostream &operator << (ostream &out, const multiset &a) { int i = 0; for (const T &x: a) { out << x << (i == len(a)-1 ? "" : " "); i++; } return out; } inline void print() { cout << "\n"; } template inline void print(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << " "; print(u...); } inline void dmp() { dout << endl; } template inline void dmp(const T &t, const U &...u) { dout << t; if (sizeof...(u)) dout << " "; dmp(u...); } } using namespace io; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } void solve(); constexpr int INF = 1e9; constexpr ll LINF = 1e18; constexpr int MOD = 1e9+7; //constexpr int MOD = 998244353; int main() { cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15); solve(); return 0; } #pragma endregion int n, m, x; vl dijkstra(const vector &g, int s) { vl d(n, LINF); vl rem(n, 0); priority_queue> q; d[s] = 0; q.emplace(make_pair(0, s)); while (!q.empty()) { auto [time, v] = q.top(); q.pop(); if (d[v] < time) continue; for (auto &[nv, value, t]: g[v]) { ll cost = t; ll wt = 0; if (rem[v] < value) { wt = (value-rem[v]+x-1)/x; cost += wt; } ll r = rem[v] + wt*x-value; if (d[nv] > d[v] + cost or (d[nv] == d[v]+cost and rem[nv] < r)) { d[nv] = d[v] + cost; rem[nv] = r; q.emplace(make_pair(d[nv], nv)); } } } return d; } void solve() { cin >> n >> m >> x; vector graph(n); rep(_, m) { int u, v, c, t; cin >> u >> v >> c >> t; u--; v--; graph[u].emplace_back(v, c, t); graph[v].emplace_back(u, c, t); } ll res = dijkstra(graph, 0)[n-1]; print(res == LINF ? -1 : res); }