#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 2e4+10,M = 2e5+10;
int h[N], e[M], w[M], ne[M], idx;
int color[N];
int n,m;
void add(int a, int b, int c) // 添加一条边a->b,边权为c
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool dfs(int u,int c,int mid)
{
color[u] = c;
for(int i = h[u];~i;i = ne[i])
{
if(w[i] <= mid)continue;
int j = e[i];
if(!color[j])
{
if(!dfs(j,3-c,mid))return false;
}
else if(color[j] == c)return false;
}
return true;
}
bool check(int mid)
{
memset(color,0,sizeof color);
for(int i = 1;i<=n;i++)
{
if(!color[i])
{
if(!dfs(i,1,mid))return false;
}
}
return true;
}
int main()
{
memset(h, -1, sizeof h);
cin>>n>>m;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
int l = 0,r = 1e9;
while(l<r)
{
int mid = l + r >> 1;
if(check(mid))r = mid;
else l = mid + 1;
}
cout<<l<<endl;
return 0;
}