const
	MAXN = 1000010;
	MAXM = 1000010;

type
	TE = record
		a, b, c: longint;
	end;
	TV = record
		p, r: longint;
	end;

var
	e: array [0..MAXM] of TE;
	v: array [0..MAXN] of TV;
	f, nx: array [0..MAXM] of longint;

function getp(x: longint): longint;
begin
	if v[x].p <> x then v[x].p := getp(v[x].p);
	getp := v[x].p;
end;

var
	n, m, a, b, i, j, result: longint;
	
begin
//	assign(input, 'C.in'); reset(input);
//	assign(output, 'C.out'); rewrite(output);
	
	readln(n, m);
	for i := 0 to m - 1 do
	begin
		readln(e[i].a, e[i].b, e[i].c);
		dec(e[i].a); dec(e[i].b);
	end;
	for i := 0 to 1000000 do
	begin
		f[i] := -1;
	end;
	for i := 0 to m - 1 do
	begin
		nx[i] := f[e[i].c];
		f[e[i].c] := i;
	end;
	for i := 0 to n - 1 do
	begin
		v[i].p := i;
		v[i].r := 0;
	end;
	result := -1;
	for j := 0 to 1000000 do
	begin
		i := f[j];
		while i <> -1 do
		begin
			a := getp(e[i].a);
			b := getp(e[i].b);
			if a <> b then
			begin
				if v[a].r > v[b].r then
				begin
					v[b].p := a;
				end else
				begin
					v[a].p := b;
					if v[a].r = v[b].r then
					begin
						inc(v[b].r);
					end;
				end;
			end;
			if getp(0) = getp(n - 1) then
			begin
				result := e[i].c;
				break;
			end;
			i := nx[i];
		end;
		if result <> -1 then break;
	end;
	writeln(result);
	
	close(output);
end.
