#include <bits/stdc++.h>
using namespace std;
int n,m;
struct edge{
int u;
int v;
int t;
};
int father[100006];
edge e[100006];
void Init(int n){
for(int i=0;i<=n;i++){
father[i]=i;
}
}
int findfather(int x){
int a=x;
while(x!=father[x]){
x=father[x];
}
while(father[a]!=a){
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
bool cmp(edge e1,edge e2){
return e1.t<e2.t;
}
int main(){
cin>>n>>m;
Init(n);
for(int i=1;i<=m;i++){
int u,v,t;
cin>>u>>v>>t;
e[i].u=u;
e[i].v=v;
e[i].t=t;
}
sort(e+1,e+m+1,cmp);
int num=0;
for(int i=1;i<=m;i++){
int faA=findfather(e[i].u);
int faB=findfather(e[i].v);
if(faA!=faB){
father[faA]=faB;
num++;}
if(num==n-1){
cout<<e[num].t;
return 0;}
}
cout<<-1;
return 0;
}