#include<bits/stdc++.h>
using namespace std;
const int maxn=2e4+10,maxm=1e5+10;
struct node{
int v,nxt,w;
}e[maxm*2];
int h[maxn],tot,n,m;
int c[maxn];
void add(int u,int v,int w){
e[++tot].nxt=h[u];
e[tot].v=v;
e[tot].w=w;
h[u]=tot;
}
bool dfs(int u,int col,int mid){
c[u]=col;
for(int i=h[u];i!=-1;i=e[i].nxt){
if(e[i].w<=mid) continue;
int v=e[i].v;
if(c[v]){
if(col==c[v]) return 0;
}else if(!dfs(v,3-col,mid)) return 0;
}
return 1;
}
bool check(int mid){
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
if(!dfs(i,1,mid)&&!c[i]) return 0;
return 1;
}
int main(){
memset(h,-1,sizeof(h));
scanf("%d%d",&n,&m);
for(int i=1,v,u,w;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,w);
}
int l=0,r=1e9;
while(l<r){
int mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<r<<endl;
return 0;
}