#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

using P = pair<Int, Int>;
using PP = pair<P, Int>;
Int n, m;
vector<PP> v;
vector<Int> d;

Int getmin(Int p){
	Int ret = 1e18;
	for(Int i=p; i>0; i-=i&-i) ret = min(ret, d[i]);
	return ret;
}
void update(Int p, Int q){
	for(Int i=p; i<=n; i+=i&-i) d[i] = min(d[i], q);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n >> m;
	v.resize(m);
	for(Int i=0; i<m; ++i){
		Int l, r, c; cin >> l >> r >> c;
		v[i] = PP(P(l, r), c);
	}
	sort(v.rbegin(), v.rend());
	d.resize(n+1, 1e18);
	d[n] = 0;
	for(Int i=0; i<m; ++i){
		Int l = v[i].first.first;
		Int r = v[i].first.second;
		Int c = v[i].second;
		Int dr = getmin(r);
		update(l, dr+c);
	}
	if(d[1] == 1e18) cout << -1 << "\n";
	else cout << d[1] << "\n";
}