#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info{
	Info(int arg_to,int arg_time){
		to = arg_to;
		time = arg_time;
	}
	int to,time;
};

struct Data{
	Data(int arg_node_id,int arg_current_time){
		node_id = arg_node_id;
		current_time = arg_current_time;
	}
	bool operator<(const struct Data &arg) const{
		return current_time > arg.current_time;
	}
	int node_id,current_time;
};

vector<Info> V[100000];
int min_time[100000];

int main(){

	int N,M;
	scanf("%d %d",&N,&M);

	int from,to,time;
	for(int i = 0; i < M; i++){
		scanf("%d %d %d",&from,&to,&time);
		from--;
		to--;
		V[from].push_back(Info(to,time));
	}

	for(int i = 0; i < N; i++)min_time[i] = BIG_NUM;

	min_time[0] = 0;

	priority_queue<Data> Q;
	Q.push(Data(0,0));

	int ans = -1;

	while(!Q.empty()){

		if(Q.top().node_id == N-1){
			ans = max(ans,Q.top().current_time);
			Q.pop();
		}else if(Q.top().current_time > min_time[Q.top().node_id]){
			Q.pop();
		}else{

			for(int i = 0; i < V[Q.top().node_id].size(); i++){
				if(Q.top().current_time > V[Q.top().node_id][i].time)continue;

				if(min_time[V[Q.top().node_id][i].to] > V[Q.top().node_id][i].time){
					min_time[V[Q.top().node_id][i].to] = V[Q.top().node_id][i].time;
					Q.push(Data(V[Q.top().node_id][i].to,V[Q.top().node_id][i].time));
				}else if(V[Q.top().node_id][i].to == N-1){
					Q.push(Data(V[Q.top().node_id][i].to,V[Q.top().node_id][i].time));
				}
			}
			Q.pop();
		}
	}

	printf("%d\n",ans);

	return 0;
}