#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define INF LLONG_MAX/5
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
pair<P,int> p[100000];
int x[100000];
priority_queue<P,vector<P>,greater<P>> q;
signed main(){
	int n,m,z=1,zz=0;
	cin>>n>>m;
	f(i,n){
		if(i)x[i]=INF;
	}
	f(i,m){
		cin>>p[i].F.F>>p[i].F.S>>p[i].S;
		p[i].F.S--;
		p[i].F.F--;
	}
	sort(p,p+m);
	while(z<n){
		for(;zz<m&&p[zz].F.F<z;zz++){
			q.push(P(p[zz].S+x[p[zz].F.F],zz));
		}
		if(q.empty()){
			puts("-1");
			return 0;
		}
		P pa=q.top();
		q.pop();
		for(;z<=p[pa.S].F.S;z++){
			x[z]=pa.F;
		}
	}
	cout<<x[n-1]<<endl;
}
