#include<cstring>
#include<iostream>
using namespace std;
const int N = 20010, M = 2e5 + 10;
int n, m;
int e[M], ne[M], w[M], h[N], idx;
int color[N];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
bool dfs(int u, int c, int mid) {
color[u] = c;
for(int i = h[u]; ~i; i = ne[i]) {
if(w[i] < mid) continue; // 注意这块一定不能是<=
int j = e[i];
if(color[j]) {
if(color[j] == color[u])
return false;
}
else if(!dfs(j, 3 - c, mid))
return false;
}
return true;
}
bool check(int mid) {
memset(color, 0, sizeof color);
for(int i = 1; i <= n; i ++)
if(!color[i])
if(!dfs(i, 1, mid))
return false;
return true;
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while(m --) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e9;
while(l + 1 < r) {
int mid = l + r >> 1;
if(check(mid)) r = mid;
else l = mid;
}
cout << l << endl;
return 0;
}