#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define L first.first
#define R first.second
#define C second

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int N, M;
	vector<int> dis;
	vector<pair<pair<int,int>,int>> in;
	vector<vector<pair<int,int>>> z;
	priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> Q;
	
	cin>>N>>M;
	
	in.resize(M);
	z.resize(N+10);
	dis.resize(N+10,INF);
	
	for(int i = 0; i < M; i++){
		cin>>in[i].L>>in[i].R>>in[i].C;
		z[in[i].L].push_back(make_pair(in[i].R, in[i].C));
	}
	
	sort(in.begin(), in.end());
	
	dis[1] = 0;
	int c = 0;
	
	for(int i = 0; i < z[1].size(); i++){
		Q.push(make_pair(z[1][i].second, z[1][i].first));
	}
	
	for(int i = 2; i <= N; i++){
		pair<int,int> t;
		
		t = Q.top();
		
		while(t.second < i){
			Q.pop();
			if(Q.size() == 0){
				cout<<-1<<endl;
				return 0;
			}
			
			t = Q.top(); 
		}
		
		dis[i] = t.first;
		
		for(int j = 0; j < z[i].size(); j++){
			Q.push(make_pair(z[i][j].second + dis[i], z[i][j].first));
		}
	}
	
	
	if(dis[N] != INF)cout<<dis[N]<<endl;
	else cout<<-1<<endl;
	
	return 0;
}