#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const long long INF = 2e18;
const int N = 2e5 + 3;
int n, m, D;
vector<long long> d[N];
vector<pair<int, int>> adj[N];
struct node {
long long du; int u, j;
bool operator< (const node &other) const {
return du > other.du;
}
};
main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> m >> D;
for (int i = 1, u, v, c; i <= m; ++i) {
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
if (n == 1) {
cout << 0;
return 0;
}
for (int i = 1; i <= n; ++i)
d[i].assign(adj[i].size(), INF);
priority_queue<node> pq;
for (int j = 0; j < adj[1].size(); ++j) {
if (mini(d[1][j], 1LL * adj[1][j].se)) {
pq.push({d[1][j], 1, j});
}
}
while (pq.size()) {
long long du = pq.top().du;
int v = pq.top().u;
int j = pq.top().j;
pq.pop();
if (du != d[v][j]) continue;
int u = adj[v][j].fi;
int len = adj[v][j].se;
if (u == n) {
cout << du;
return 0;
}
for (int j = 0; j < adj[u].size(); ++j) {
if (len + D > adj[u][j].se) continue;
if (mini(d[u][j], du + adj[u][j].se)) {
pq.push({d[u][j], u, j});
}
}
}
cout << -1;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Ugc2Vjb25kCgp0ZW1wbGF0ZSA8dHlwZW5hbWUgVDEsIHR5cGVuYW1lIFQyPiBib29sIG1pbmkoVDEgJmEsIFQyIGIpIHsKICBpZiAoYSA+IGIpIHthID0gYjsgcmV0dXJuIHRydWU7fSByZXR1cm4gZmFsc2U7Cn0KdGVtcGxhdGUgPHR5cGVuYW1lIFQxLCB0eXBlbmFtZSBUMj4gY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dLnNlKSkgewogICAgICAgICAgICBwcS5wdXNoKHtkWzFdW2pdLCAxLCBqfSk7CiAgICAgICAgfQogICAgfQogICAgd2hpbGUgKHBxLnNpemUoKSkgewogICAgICAgIGxvbmcgbG9uZyBkdSA9IHBxLnRvcCgpLmR1OwogICAgICAgIGludCB2ID0gcHEudG9wKCkudTsKICAgICAgICBpbnQgaiA9IHBxLnRvcCgpLmo7CiAgICAgICAgcHEucG9wKCk7CiAgICAgICAgaWYgKGR1ICE9IGRbdl1bal0pIGNvbnRpbnVlOwogICAgICAgIGludCB1ID0gYWRqW3ZdW2pdLmZpOwogICAgICAgIGludCBsZW4gPSBhZGpbdl1bal0uc2U7CiAgICAgICAgaWYgKHUgPT0gbikgewogICAgICAgICAgICBjb3V0IDw8IGR1OwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBhZGpbdV0uc2l6ZSgpOyArK2opIHsKICAgICAgICAgICAgaWYgKGxlbiArIEQgPiBhZGpbdV1bal0uc2UpIGNvbnRpbnVlOwogICAgICAgICAgICBpZiAobWluaShkW3VdW2pdLCBkdSArIGFkalt1XVtqXS5zZSkpIHsKICAgICAgICAgICAgICAgIHBxLnB1c2goe2RbdV1bal0sIHUsIGp9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgLTE7Cn0K