#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i <= (b); i ++ )
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int n, m, k, flag, ecnt; ll ans;
struct Edge { int u, v, w; } e[N];
struct DSU {
int fa[N];
inline void init() { iota(fa + 1, fa + n + 1, 1); }
int find(int u) { return u == fa[u] ? u : fa[u] = find(fa[u]); }
inline bool check(int u, int v) { return find(u) == find(v); }
inline void merge(int u, int v) {
u = find(u), v = find(v);
if (u ^ v) fa[u] = v;
}
} dsu;
int main() {
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m >> k, dsu.init();
_for (i, 1, m) cin >> e[i].u >> e[i].v >> e[i].w;
sort(e + 1, e + m + 1, [&] (Edge u, Edge v) { return u.w < v.w; });
_for (i, 1, m) if (! dsu.check(e[i].u, e[i].v)) {
dsu.merge(e[i].u, e[i].v), ecnt ++ ;
if (e[i].w > k) flag = 1, ans += e[i].w - k;
if (ecnt == n - 1) break;
}
if (flag) return cout << ans << "\n", 0;
ans = (ll)1e18 + 5;
_for (i, 1, m) ans = min(ans, 1ll * abs(k - e[i].w));
return cout << ans << "\n", 0;
}