#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n, m; long long x; cin >> n >> m >> x; using till = tuple; vector> g(n); for (int i = 0; i < m; i++) { int u, v, c, t; cin >> u >> v >> c >> t; u--, v--; g[u].push_back({v, c, t}); g[v].push_back({u, c, t}); } const long long INF = 1e18; vector> dist(n, {INF, INF}); vector vis(n); dist[0] = {0, 0}; using tlli = tuple; priority_queue, greater> pq; pq.push({0, 0, 0}); while (!pq.empty()) { auto [tt, cc, u] = pq.top(); pq.pop(); if (vis[u]) { continue; } vis[u] = true; for (auto [v, c, t] : g[u]) { long long nt = tt + t, nc = cc + c; nt += nc / x; nc %= x; if (dist[v] > make_pair(nt, nc)) { dist[v] = {nt, nc}; pq.push({nt, nc, v}); } } } if (!vis[n - 1]) { cout << -1 << endl; } else { cout << dist[n - 1].first + (dist[n - 1].second != 0) << endl; } }