#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node{
int u,v,w;
}a[100010];
bool cmp(node a,node b){
return a.w>=b.w;
}
int n,m,k,fa[1000010],x,y,s,tot;
int find(int x){
if(fa[x]==x) return x;
else fa[x]=find(fa[x]);
}
signed main(){
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
cin>>a[i].u>>a[i].v>>a[i].w;
}
for(int i=1;i<=n;i++){
fa[i]=i;
}
sort(a+1,a+1+m,cmp);
for(int i=1;i<=m;i++){
x=a[i].u;
y=a[i].v;
if(x>y) swap(x,y);
x=find(x);
y=find(y);
if(x!=y){
fa[x]=y;
tot++;
s+=a[i].w;
}
if(tot>=k or tot>=n-1) break;
}
cout<<s;
return 0;
}