#include using namespace std; using ll = long long; using ld = long double; using pii = pair; using pll = pair; using vi = vector; #define pb push_back #define eb emplace_back #define fi first #define se second #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define rep(i, a, b) for (int i = (a); i < (b); i++) mt19937 rng(random_device{}()); struct DSU { vi par, cnt; DSU(int N): par(N), cnt(N, 1) { iota(all(par), 0); } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } int merge(int x, int y) { x = root(x), y = root(y); if (x == y) return 0; if (cnt[x] < cnt[y]) swap(x, y); par[y] = x; cnt[x] += cnt[y]; return 1; } }; int main() { cin.tie(0)->sync_with_stdio(0); int n, m; cin >> n >> m; vector> edges; for (int i = 0; i < m; i++) { int a, b, w; cin >> a >> b >> w; a--,b--; edges.emplace_back(w, a, b); } sort(begin(edges), end(edges)); DSU dsu(n); for (auto [w, a, b]: edges) { if (dsu.merge(a, b)) { if (dsu.root(0) == dsu.root(n - 1)) { cout << w << endl; return 0; } } } cout << "NaN" << endl; }