/*
MST,注意只能加K条边,但是备选是M条边
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define fo(i,l,r) for(int i = l;i <= r;i++)
#define fd(i,l,r) for(int i = r;i >= l;i--)
using namespace std;
const int N = ;
ll read(){
ll x=,f=;
char ch=getchar();
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
struct edge{
int u,v;ll w;
}e[N*];
int n,m,k,cnt,head[N],f[N];ll ans;
bool cmp(edge a,edge b){return a.w > b.w;}
int findf(int x) {return f[x] == x ? f[x] : f[x] = findf(f[x]);}
int main(){
freopen("carpet.in","r",stdin);
freopen("carpet.out","w",stdout);
n =read();m=read();k=read();
int u,v;ll w;
fo(i,,m){
e[i].u=read();e[i].v=read();e[i].w=read();
}
sort(e+,e++m,cmp);
fo(i,,n) f[i] = i;
int fa,fb;
fo(i,,m){
fa = findf(e[i].u);fb=findf(e[i].v);
if(fa != fb){
f[fa] = fb,ans += e[i].w;
k--;
}
if(!k) break;
}
cout<<ans;
return ;
}