#include <bits/stdc++.h>
#define int long long
#define inf 100000000000000000
#define f first
#define s second
using namespace std;
int32_t main(){
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	int n,m;
	cin >> n >> m;
	pair <pair<int,int>,int> v[m];
	for(int i=0;i<m;++i)
		cin >> v[i].f.f >> v[i].s >> v[i].f.s ;
	sort(v,v+m);
	int d[n+1];
	for(int i=1;i<=n;i++)
		d[i]=inf;
	d[1]=0;
	set <int> s;
	s.insert(1);
	s.insert(n);
	for(int i=0;i<m;++i){
		int l=v[i].f.f,r=v[i].s,c=v[i].f.s;
		auto itl,itr,t;
		itr=s.lower_bound(r);
		itl=s.lower_bound(l);
		int x=d[*itl]+c;
		if(x>=d[*itr])
			continue;
		t=--itr;
		while(d[*t]>=x){
			if(t==s.begin()){
				s.erase(t);
				break;
			}
			t--;
			s.erase(itr);
			itr=t;
		}
		s.insert(r);
		d[r]=x;
	}
	// for(int i=1;i<=n;i++)
	// 	cout << d[i] << " ";
	if(d[n]==inf)
		d[n]=-1;
	cout << d[n] << endl;
}