#include
#include
#include
#include
#define N 100005
typedef long long LL;
inline void Read(LL &x)
{
bool f=0;register char ch=getchar();
for(x=0;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
for(;isdigit(ch);x=x*10+ch-'0',ch=getchar());
x=f?-x:x;
}
using namespace std;
struct node
{
LL x,y,z;
bool operator<(node a)const
{
return z<a.z;
}
}edge[N<<1];
LL fa[N],n,m;
LL ans=-0x7fffffff,l,r;
LL find_(LL x) {return fa[x]==x?x:fa[x]=find_(fa[x]);}
LL max(LL a,LL b) {return a>b?a:b;}
LL min(LL a,LL b) {return a>b?b:a;}
bool check(LL x)
{
for(LL i=1;i<=n;++i) fa[i]=i;
LL now=0,num=0;
for(LL i=m;i>=1;--i)
{
int fx=find_(edge[i].x),fy=find_(edge[i].y);
if(fx!=fy&&edge[i].z<=x)
{
fa[fy]=fx;
now+=edge[i].z;
num++;
if(num==n-1) break;
}
}
if(num==n-1) {ans=now;return true;}
return false;
}
int main()
{
Read(n);Read(m);
for(int i=1;i<=m;++i) Read(edge[i].x),Read(edge[i].y),Read(edge[i].z);
l=-0x7fffffff,r=0x7fffffff;
sort(edge+1,edge+1+m);
for(LL mid;l<=r;)
{
mid=(l+r)>>1;
if(check(mid)) r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}