#include<iostream>
#include<algorithm>
#include<string>
#define max 0x3f
using namespace std;
struct node
{
int x,y,w;
}edge[5000000];
bool cmp(node a,node b)
{
return a.w<b.w;
}
int f[5000000];
int get(int x)
{
return x==f[x]?x:f[x]=get(f[x]);
}
int main()
{
int max1=0;
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>edge[i].x>>edge[i].y>>edge[i].w;
}
for(int i=0;i<=n;i++)
{
f[i]=i;
}
sort(edge+1,edge+m+1,cmp);
for(int i=0;i<m;i++)
{
int x=get(edge[i].x);
int y=get(edge[i].y);
if (x==y)
{
continue;
}
f[y]=x;
if(edge[i].w>max1)
{
max1=edge[i].w;
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(i==f[i])
ans++;
}
if(ans>1)
{
cout<<"-1";
}
else cout<<max1;
return 0;
}