#include<iostream>#include<algorithm>usingnamespacestd;
struct side{ int u, v, w; }e[101000];
bool cmp(side a, side b){ return a.w<b.w; }
int fa[100010];
int find(int x){ return x==fa[x] ? x : fa[x]=find(fa[x]) ; }
int main(){
int n, m;
cin>>n>>m;
for(int i = 0; i < m; i++)cin>>e[i].u>>e[i].v>>e[i].w;
sort(e, e+m, cmp);
for(int i = 1; i <= n; i++)fa[i] = i; int cc = n-1, ans = 0;
for(int i = 0; i < m; i++){
if(find(e[i].u) != find(e[i].v)){
fa[find(e[i].u)] = find(e[i].v);
cc--; ans = max(ans, e[i].w);
}
if(!cc)break;
}
if(!cc)cout<<ans;
elsecout<<"-1";
return0;
}