#include using namespace std; #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P=pair; const ll INF=1e18; const int mod=1e9+7; struct edge{ int to; ll w; edge(int to,ll w):to(to),w(w){} }; int main(){ ll n,m; cin>>n>>m; vector>G(n); rep(i,m){ int s,t,d; cin>>s>>t>>d; s--;t--; G[s].push_back(edge(t,d)); G[t].push_back(edge(s,d)); } auto f=[&](ll W){ vectordist(n,INF); dist[0]=0; queueq; q.push(0); while(!q.empty()){ auto p=q.front();q.pop(); for(auto u:G[p]){ if(dist[u.to]==INF&&u.w>=W){ q.push(u.to); dist[u.to]=dist[p]+1; } } } return dist[n-1]; }; ll l=0,r=1e9+5; ll len=INF; while(r-l>1){ ll mid=(l+r)/2; ll dist=f(mid); if(dist!=INF){l=mid;len=dist;} else{r=mid;} } cout<