#include<bits/stdc++.h>
using namespace std;
int fa[100000];
int get_root(int u){
if(fa[u]==0){
return u;
}
int root=get_root(fa[u]);
fa[u]=root;
return root;
}
void merge(int u,int v){
int root_u=get_root(u);
int root_v=get_root(v);
if(root_u==root_v){
return;
}
fa[root_u]=root_v;
}
bool query(int u,int v){
int root_u=get_root(u);
int root_v=get_root(v);
if(root_u==root_v){
return true;
}else{
return false;
}
}
const int N=1e5;
struct road{
int x;
int y;
int t;
}r[N];
unsigned long long n;
unsigned long long m;
bool cmp(road a,road b){
return a.t<b.t;
}
int h[100000];
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>r[i].x;
cin>>r[i].y;
cin>>r[i].t;
}
sort(r+1,r+1+m,cmp);
int c = n;
for(int i=1;i<=m;i++){
bool fx =query(r[i].x,r[i].y);
if(fx==true){
continue;
}else{
merge(r[i].x,r[i].y);
-- c;
}
if (c == 1){
cout << r[i].t << endl;
return 0;
}
}
cout<<"-1";
return 0;
}