#include <bits/stdc++.h>
using i64 = long long;
using PII = std::pair<int, int>;
struct Edge { int x, y, z; } edge[200010];
int fa[100010], n, m, ans, res;
int a, b;
int get(int x) {
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
int main() {
freopen("zxscs.in", "r", stdin);
freopen("zxscs.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> m;
for (int i = 1; i <= m; ++ i) {
auto& [x, y, z] = edge[i];
std::cin >> x >> y >> z;
if (z == -1) a = x, b = y;
}
std::cin >> ans;
for (int i = 1; i <= 10000010; ++ i) {
res = 0;
for (auto& j : edge)
if (j.x == a && j.y == b) j.z = i;
std::sort(edge + 1, edge + m + 1, [](Edge a, Edge b) {
return a.z < b.z;
});
for (int j = 1; j <= m; ++ j) fa[j] = j;
for (int j = 1; j <= m; ++ j) {
int x = get(edge[j].x);
int y = get(edge[j].y);
if (x == y) continue;
fa[x] = y;
res += edge[j].z;
}
if (ans == res) return std::cout << i << '\n', 0;
}
return 0;
}