#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int q[40001],x,y;
struct node
{
int a,b,c;
}f[100001];
bool cmp(node x,node y)
{
return x.c>y.c;
}
int find(int x)
{
if (q[x]==x) return x;
else
{
q[x]=find(q[x]);
return q[x];
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c);
sort(f+1,f+1+m,cmp);
memset(q,0,sizeof(q));
for (int i=1;i<=2*n;i++) q[i]=i;
for (int i=1;i<=m;i++)
{
x=find(f[i].a);
y=find(f[i].b);
if (x==y)
{
printf("%d",f[i].c);
return 0;
}
q[y]=find(f[i].a+n);
q[x]=find(f[i].b+n);
}
cout<<0;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node{
int y,z,next;
}e[200020];
int tot,item,n,m;
int head[20005],b[20005],a[100005];
bool pd;
int cmp(int a,int b) { return a<b;}
void insert(int x,int y,int z)
{
e[++tot].y=y;
e[tot].z=z;
e[tot].next=head[x];
head[x]=tot;
}
void dfs(int x,int data)
{
if(pd==false) return;
b[x]=data;
for(int i=head[x];i!=0;i=e[i].next)
{
if(e[i].z>item)
{
if(b[e[i].y]==-1)dfs(e[i].y,1-data);
else
if(b[e[i].y]==data){pd=false; return;}
}
}
}
bool work(int midd)
{
memset(b,-1,sizeof(b));
pd=true;
item=a[midd];
for(int i=1;i<=n;i++)
if(b[i]==-1) dfs(i,0);
return pd;
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,z;
tot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
insert(x,y,z);
insert(y,x,z);
a[i+1]=z;
}
a[1]=0;
sort(a+1,a+2+m,cmp);
int l=1,r=m+1,mid;
while(l<r)
{
mid=(l+r)/2;
if(work(mid)==true)r=mid;
else l=mid+1;
}
printf("%d\n",a[l]);
}