#include<bits/stdc++.h>
using namespace std;
namespace io{
char buf[1<<20],*p1=buf,*p2=buf;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin)),p1==p2?EOF:*p1++)
int read(){
int x=0,f=0;char c=gc();
while(c< '0'||c> '9') f|=c=='-',c=gc();
while(c>='0'&&c<='9') x=x*10+(c^48),c=gc();
return f?-x:x;
}
char pbuf[1<<20],*pp=pbuf;
#define flush() (fwrite(pbuf,1,pp-pbuf,stdout),pp=pbuf)
#define pc(ch) (pp==pbuf+(1<<20)&&flush(),*pp++=(ch))
class Flush{public:~Flush(){flush();}}_;
void write(long long x,char ch=0){
if(x<0) pc('-'),x=-x;
static int st[35];int top=0;
do st[top++]=x%10,x/=10;while(x);
while(top) pc(st[--top]^48);if(ch) pc(ch);
}
void putc(char ch){pc(ch);}
}
#define ll long long
#define N 200009
tuple<int,int,int>e[N];
int f[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int main(){
// freopen("in.in","r",stdin);
int n,m,k,minn=-1;
n=io::read();
m=io::read();
k=io::read();
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++){
int u,v,w;
u=io::read();
v=io::read();
w=io::read();
e[i]={w,u,v};
if(minn==-1) minn=abs(w-k);
else minn=min(minn,abs(w-k));
}
sort(e+1,e+m+1);
int las=0;ll ans=0;
for(int i=1;i<=m;i++){
int u,v,w;
tie(w,u,v)=e[i];
if(find(u)!=find(v)){
f[find(u)]=find(v);
if(w>k) ans+=w-k;
las=w;
}
}
if(las<=k) io::write(minn);
else io::write(ans);
return 0;
}