/// Author : Nguyễn Thái Sơn - K18 - KHMT - UIT
/// Training ICPC 2024
#include<bits/stdc++.h>
/// #pragma GCC optimize("O3,unroll-loops")
/// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define fi first
#define se second
#define TASK "test"
#define pb push_back
#define EL cout << endl
#define Ti20_ntson int main()
#define in(x) cout << x << endl
#define all(x) (x).begin(),(x).end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define cntbit(x) __builtin_popcount(x)
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
const int N = 1e4 + 5;
const int oo = INT_MAX;
const int mod = 1e9 + 7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, m;
tuple<int, int, int> Canh[N];
vector<int> dsk[N];
bool visited[N];
inline void Read_Input() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
Canh[i] = {u, v, w};
}
}
void dfs(int u, int p) {
visited[u] = true;
for (int v : dsk[u])
if (v != p)
dfs(v, u);
}
bool Check(int val) {
/// B2 : tạo đồ thị mới với những cạnh có trọng số <= x
/// Bắt đầu làm là chưa có gì
FOR(i, 1, n)
visited[i] = false, dsk[i].clear();
FOR(i, 1, m) {
auto[u, v, w] = Canh[i];
if (w <= val) {
dsk[u].push_back(v);
dsk[v].push_back(u);
}
}
dfs(1, 1);
if (visited[n] == true) return true;
return false;
}
inline void Solve() {
/// B1 : chặt nhị phân giá trị x để kiếm tra
int l = 1, r = 1e9;
int Ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (Check(mid) == true) {
/// Check gia tri 5 = dung
/// [5, 6, 7 ... , 1e9] = dung
/// [1, 4]
r = mid - 1;
Ans = mid;
}
else l = mid + 1;
}
cout << Ans;
}
Ti20_ntson {
// freopen(TASK".INP","r",stdin);
// freopen(TASK".OUT","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
// cin >> T;
while (T -- ) {
Read_Input();
Solve();
}
}
Ly8vIEF1dGhvciA6IE5ndXnhu4VuIFRow6FpIFPGoW4gLSBLMTggLSBLSE1UIC0gVUlUCi8vLyBUcmFpbmluZyBJQ1BDIDIwMjQKCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CgovLy8gI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8zLHVucm9sbC1sb29wcyIpCi8vLyAjcHJhZ21hIEdDQyB0YXJnZXQoImF2eDIsYm1pLGJtaTIsbHpjbnQscG9wY250IikKCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBUQVNLICJ0ZXN0IgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIEVMIGNvdXQgPDwgZW5kbAojZGVmaW5lIFRpMjBfbnRzb24gaW50IG1haW4oKQojZGVmaW5lIGluKHgpIGNvdXQgPDwgeCA8PCBlbmRsCiNkZWZpbmUgYWxsKHgpICh4KS5iZWdpbigpLCh4KS5lbmQoKQojZGVmaW5lIGdldGJpdCh4LCBpKSAoKCh4KSA+PiAoaSkpICYgMSkKI2RlZmluZSBjbnRiaXQoeCkgX19idWlsdGluX3BvcGNvdW50KHgpCiNkZWZpbmUgRk9SKGksbCxyKSBmb3IgKGludCBpID0gbDsgaSA8PSByOyBpKyspCiNkZWZpbmUgRk9SRChpLGwscikgZm9yIChpbnQgaSA9IGw7IGkgPj0gcjsgaS0tKQojZGVmaW5lIERlYnVnKGEsbikgZm9yIChpbnQgaSA9IDE7IG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