#include<cstdio>
#define G *++ptr
const int N=500007,inf=0x3f3f3f3f;
char buf[N*60],*ptr=buf-1;
int _(){
int x=0,c=G;
while(c<48)c=G;
while(c>47)x=x*10+c-48,c=G;
return x;
}
bool ei[N*4];
int n,m,es[N*6],enx[N*6],ev[N*6],e0[N],e1[N],ep=2,id[N],idp;
void ae(int*e,int a,int b,int c){
es[ep]=b;enx[ep]=e[a];ev[ep]=c;e[a]=ep++;
es[ep]=a;enx[ep]=e[b];ev[ep]=c;e[b]=ep++;
}
int dfn[N],low[N],tk=0;
void mins(int&a,int b){if(a>b)a=b;}
void maxs(int&a,int b){if(a<b)a=b;}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
void tj(int w){
dfn[w]=low[w]=++tk;
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(!u)continue;
if(!dfn[u]){
es[i^1]=0;
tj(u);
es[i^1]=w;
mins(low[w],low[u]);
if(low[u]>dfn[w])ei[i>>1]=1;
}else mins(low[w],dfn[u]);
}
}
void f1(int w){
id[w]=idp;
dfn[w]=0;
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(!ei[i>>1]&&dfn[u])f1(u);
}
}
int v0[N],v02[N],v1[N],v2[N],vu[N],v,ans=0;
void f2(int w,int pa){
v0[w]=v02[w]=v1[w]=v2[w]=vu[w]=inf;
for(int i=e1[w];i;i=enx[i]){
int u=es[i];
if(u==pa)continue;
f2(u,w);
v=min(v0[u],ev[i]);
v2[w]=max(min(v2[w],v),min(v1[w],v1[u]));
v1[w]=max(min(v1[w],v),min(v0[w],v1[u]));
if(v<=v0[w])v02[w]=v0[w],v0[w]=v;
else mins(v02[w],v);
}
}
void f3(int w,int pa){
v=min(vu[w],v2[w]);
maxs(ans,v);
for(int i=e1[w];i;i=enx[i]){
int u=es[i];
if(u==pa)continue;
vu[u]=min(min(ev[i],vu[w]),min(v0[u],ev[i])==v0[w]?v02[w]:v0[w]);
f3(u,w);
}
}
int main(){
fread(buf,1,sizeof(buf),stdin)[buf]=0;
n=_();m=_();
for(int i=0,a,b,c;i<m;++i){
a=_();b=_();c=_();
if(a==b)continue;
ae(e0,a,b,c);
}
tj(1);for(int i=1;i<=n;++i)if(dfn[i]){
++idp;
f1(i);
}
for(int i=2;i<ep;i+=2)if(ei[i>>1])ae(e1,id[es[i]],id[es[i^1]],ev[i]);
f2(1,0);
f3(1,0);
if(ans==inf)ans=-1;
printf("%d",ans);
return 0;
}