/**
* author: mamion
* created: Sunday 2024-10-27
**/
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include<cpp-dump-main/cpp-dump.hpp>
#define debug(...) cpp_dump(__VA_ARGS__)
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);
CPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cpp_dump::log_label::filename());
CPP_DUMP_SET_OPTION_GLOBAL(enable_asterisk, true);
#else
#define debug(...)
#endif // LOCAL
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int inf = 1e9;
const int N = 1e4 + 10, M = 1e3 + 10;
int numNode, numEdge, moneyLim;
vector<tuple<int, int, int>> adj[N];
pair<int, int> f[N][2];
void dijkstra() {
memset(f, 0x3f, sizeof f);
f[1][0] = f[1][1] = {0, -moneyLim};
priority_queue<tuple<int, int, int, int>> pq;
pq.push({0, moneyLim, 1, 0}); pq.push({0, moneyLim, 1, 1});
while (pq.size()) {
int dist = -get<0>(pq.top()); int money = get<1>(pq.top());
int u = get<2>(pq.top()); int state = get<3>(pq.top());
pq.pop();
if (dist != f[u][state].first || -money != f[u][state].second) continue;
for (auto p : adj[u]) {
int v = get<0>(p), c = get<1>(p), d = get<2>(p);
if (state == 1) c++; else d++;
// go by bike here
int moneyBike = money, wBike = 0;
if (moneyBike < c) {
moneyBike = min(moneyLim, money + (c - money + 99 - 1) / 99 * 99);
wBike = (c - money + 99 - 1) / 99;
}
moneyBike -= c;
if (f[v][0] > make_pair(f[u][state].first + wBike + c, -moneyBike)) {
f[v][0] = {f[u][state].first + wBike + c, -moneyBike};
pq.push({-f[v][0].first, moneyBike, v, 0});
}
// go by bus here
int moneyBus = money, wBus = 0;
if (moneyBus < d) {
moneyBus = min(moneyLim, money + (d - money + 99 - 1) / 99 * 99);
wBus = (d - money + 99 - 1) / 99;
}
moneyBus -= d;
if (f[v][1] > make_pair(f[u][state].first + wBus + d, -moneyBus)) {
f[v][1] = {f[u][state].first + wBus + d, -moneyBus};
pq.push({-f[v][1].first, moneyBus, v, 1});
}
}
}
}
void solve() {
cin >> numNode >> numEdge;
for (int i = 0; i < numEdge; i++) {
int u, v, c, d; cin >> u >> v >> c >> d;
adj[u].push_back({v, c, d});
adj[v].push_back({u, c, d});
}
cin >> moneyLim;
dijkstra();
int ans = inf;
ans = min(ans, f[numNode][0].first);
ans = min(ans, f[numNode][1].first);
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#else
#define file "name"
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
#endif // LOCAL
int T; T = 1; if (0) cin >> T;
for (int i = 1; i <= T; i++)
{
solve();
}
}
LyoqCiAqICAgIGF1dGhvcjogIG1hbWlvbgogKiAgICBjcmVhdGVkOiBTdW5kYXkgMjAyNC0xMC0yNwoqKi8KCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgTE9DQUwKI2luY2x1ZGU8Y3BwLWR1bXAtbWFpbi9jcHAtZHVtcC5ocHA+CiNkZWZpbmUgZGVidWcoLi4uKSBjcHBfZHVtcChfX1ZBX0FSR1NfXykKQ1BQX0RVTVBfU0VUX09QVElPTl9HTE9CQUwobWF4X2xpbmVfd2lkdGgsIDEwMCk7CkNQUF9EVU1QX1NFVF9PUFRJT05fR0xPQkFMKGxvZ19sYWJlbF9mdW5jLCBjcHBfZHVtcDo6bG9nX2xhYmVsOjpmaWxlbmFtZSgpKTsKQ1BQX0RVTVBfU0VUX09QVElPTl9HTE9CQUwoZW5hYmxlX2FzdGVyaXNrLCB0cnVlKTsKI2Vsc2UKI2RlZmluZSBkZWJ1ZyguLi4pCiNlbmRpZiAvLyBMT0NBTAoKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgdW5zaWduZWQgbG9uZyBsb25nIHVsbDsKdHlwZWR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9IG1vbmV5LCB3QmlrZSA9IDA7CiAgICAgICAgICAgIGlmIChtb25leUJpa2UgPCBjKSB7CiAgICAgICAgICAgICAgICBtb25leUJpa2UgPSBtaW4obW9uZXlMaW0sIG1vbmV5ICsgKGMgLSBtb25leSArIDk5IC0gMSkgLyA5OSAqIDk5KTsKICAgICAgICAgICAgICAgIHdCaWtlID0gKGMgLSBtb25leSArIDk5IC0gMSkgLyA5OTsKICAgICAgICAgICAgfQogICAgICAgICAgICBtb25leUJpa2UgLT0gYzsKICAgICAgICAgICAgaWYgKGZbdl1bMF0gPiBtYWtlX3BhaXIoZlt1XVtzdGF0ZV0uZmlyc3QgKyB3QmlrZSArIGMsIC1tb25leUJpa2UpKSB7CiAgICAgICAgICAgICAgICBmW3ZdWzBdID0ge2ZbdV1bc3RhdGVdLmZpcnN0ICsgd0Jpa2UgKyBjLCAtbW9uZXlCaWtlfTsKICAgICAgICAgICAgICAgIHBxLnB1c2goey1mW3ZdWzBdLmZpcnN0LCBtb25leUJpa2UsIHYsIDB9KTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gZ28gYnkgYnVzIGhlcmUKICAgICAgICAgICAgaW50IG1vbmV5QnVzID0gbW9uZXksIHdCdXMgPSAwOwogICAgICAgICAgICBpZiAobW9uZXlCdXMgPCBkKSB7CiAgICAgICAgICAgICAgICBtb25leUJ1cyA9IG1pbihtb25leUxpbSwgbW9uZXkgKyAoZCAtIG1vbmV5ICsgOTkgLSAxKSAvIDk5ICogOTkpOwogICAgICAgICAgICAgICAgd0J1cyA9IChkIC0gbW9uZXkgKyA5OSAtIDEpIC8gOTk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbW9uZXlCdXMgLT0gZDsKICAgICAgICAgICAgaWYgKGZbdl1bMV0gPiBtYWtlX3BhaXIoZlt1XVtzdGF0ZV0uZmlyc3QgKyB3QnVzICsgZCwgLW1vbmV5QnVzKSkgewogICAgICAgICAgICAgICAgZlt2XVsxXSA9IHtmW3VdW3N0Y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