#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cassert>

using namespace std;

const int MAXN = 1000010;
const int MAXM = 1000010;

struct E {
	int a, b, c;
	bool operator<(const E &w) const {
		return c < w.c;
	}
} e[MAXM];
struct V {
	int p, r;
} v[MAXN];
int f[MAXM];
int nx[MAXM];

int getp(int x) {
	if (v[x].p != x) v[x].p = getp(v[x].p);
	return v[x].p;
}

int main() {
//	freopen("C.in", "r", stdin);
//	freopen("C.out", "w", stdout);
	
	int n, m;
	scanf("%d %d", &n, &m);
	assert((2 <= n) && (n <= 1000000) && (2 <= m) && (m <= 1000000));
	for (int i = 0; i < m; ++i) {
		scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].c);
		assert((1 <= e[i].a) && (e[i].a <= n) && (1 <= e[i].b) && (e[i].b <= n) && (0 <= e[i].c) && (e[i].c <= 1000000));
		assert(e[i].a != e[i].b);
		--e[i].a; --e[i].b;
	}
	for (int i = 0; i <= 1000000; ++i) {
		f[i] = -1;
	}
	for (int i = 0; i < m; ++i) {
		nx[i] = f[e[i].c];
		f[e[i].c] = i;
	}
	for (int i = 0; i < n; ++i) {
		v[i].p = i;
		v[i].r = 0;
	}
	int result = -1;
	for (int j = 0; j <= 1000000; ++j) {
		int i = f[j];
		while (i != -1) {
			int a, b;
			if ((a = getp(e[i].a)) != (b = getp(e[i].b))) {
				if (v[a].r > v[b].r) {
					v[b].p = a;
				} else {
					v[a].p = b;
					if (v[a].r == v[b].r) {
						++v[b].r;
					}
				}
			}
			if (getp(0) == getp(n - 1)) {
				result = e[i].c;
				break;
			}
			i = nx[i];
		}
		if (result != -1) {
			break;
		}
	}
	assert(result != -1);
	printf("%d\n", result);
	return 0;
}
