#include<bits/stdc++.h>
using namespace std;
int n, m, fa[10010];
long long ans, s;
struct node {
int x, y, z;
}e[200200];
bool operator <(node a, node b) {
return a.z < b.z;
}
int get(int x) {
if(x == fa[x]) {
return x;
}
return fa[x] = get(fa[x]);
}
int dfs(int x, int big) {
for(int i = 1; i <= m; i ++) {
}
return 0;
}
int main() {
freopen("zxscs.in", "r", stdin);
freopen("zxscs.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; i ++) {
cin >> e[i].x >> e[i].y >> e[i].z;
if(e[i].z == -1) {
e[i].z = 0;
}
}
cin >> s;
sort(e + 1, e + m + 1);
for(int i = 1; i <= n; i ++) {
fa[i] = i;
}
ans = 0;
for(int i = 1; i <= m; i ++) {
int xx = get(e[i].x), yy = get(e[i].y);
if(xx == yy) {
continue;
}
fa[xx] = yy;
ans += e[i].z;
}
cout << s - ans;
return 0;
}