#include #include using namespace std; using namespace atcoder; #define ll long long #define rep(i,a,b) for(int i=(a);i<(b);i++) #define repl(i,a,b) for(ll i=(a);i<(b);i++) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() template bool chmin(T &a,T b){if(a>b){a=b;return true;} return false;} template bool chmax(T &a,T b){if(a> n >> m; vector> e; rep(i,0,m){ int u,v,h; cin >> u >> v >> h; u--,v--; e.push_back({u,v,h}); } ll ok=0,ng=2e9; while(ng-ok>1){ int mid=(ok+ng)/2; dsu d(n); for(auto [u,v,h]:e){ if(h>=mid){ d.merge(u,v); } } if(d.same(0,n-1)) ok=mid; else ng=mid; } if(ok == 0) cout << "NaN" << "\n"; else cout << ok << "\n"; }