#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int f[N];
struct node{
int x,y,t;
}e[N];
bool cmp(node a,node b){
return a.t<b.t;
}
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
bool check(){
//cout << "welcome to the check program!:" << endl;
//for(int i=1;i<=n;i++) cout << find(i) << " ";
//cout << endl;
for(int i=1;i<=n;i++){
if(find(i)!=find(1)) return false;
}
return true;
}
void get(int x,int y){
int fx=find(x),fy=find(y);
f[y]=fx;
}
void output(){
for(int i=1;i<=m;i++){
cout << e[i].x << " " << e[i].y <<" " << e[i].t << endl;
}
}
int main(){
//freopen("P1111_1.in","r",stdin);
cin >> n >> m;
for(int i=1;i<=m;i++) cin >> e[i].x >> e[i].y >> e[i].t;
for(int i=1;i<=m;i++) f[i]=i;
sort(e+1,e+m+1,cmp);
//output();
for(int i=1;i<=m;i++){
get(e[i].x,e[i].y);
if(check()){
cout << e[i].t;
exit(0);
}
}
cout << -1;
return 0;
}
代码呈上orz