#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#define pb push_back
#define For(i,s,e) for (ll i=(s); i<(e); i++)
#define Debug_array(a,n) for (ll i=(0); i<(n); i++) cout<<a[i]<<" "
#define Foe(i,s,e) for (ll i=(s); i<=(e); i++)
#define Fod(i,s,e) for (ll i=(s)-1; i>=(e); i--)
#define pii pair<ll,ll>
#define fi first
#define se second
#define endl "\n"
#define mp make_pair
#define big_prime 15486277
#define bigger_prime 179424697
#define biggest_prime 32416188691
//#define random_shuffle(indices.begin(), indices.end());
//std::random_device rd;
//std::mt19937 g(rd());
//std::shuffle(v.begin(), v.end(), g);
using namespace __gnu_pbds; 
using namespace std;
template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
// template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
 
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
    template <class Arg1, class... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args) {
        const char* sep = strchr(names + 1, ',');
        cerr.write(names, sep - names) << " : " << arg1 << "  ";
        __f(sep + 1, args...);
    }
#else
#define trace(...) 0
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE


typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; 

#define Mod 1000000007
#define MAX 100010

struct SegmentTree{
	struct Node
	{
		ll x,l;
		Node() {
			x = 1e18;
			l = 1e18;
		};
		Node(ll x,ll l): x(x) , l(l) {};
	};
	ll max_n;
	Node* tree;
	SegmentTree(ll max_n){
		this->max_n = max_n;
		tree = new Node[4*max_n+2];
		For(i,0,4*max_n+2) tree[i] = Node(1e18,1e18);
	}

	void propagate(ll ti){
		tree[ti].x = min(tree[ti].x,tree[ti].l);
		tree[2*ti].l = min(tree[2*ti].l,tree[ti].l);
		tree[2*ti+1].l = min(tree[2*ti+1].l,tree[ti].l);
	}

	void up(ll ti){
		tree[ti].x = min(min(tree[2*ti].x,tree[2*ti].l) , min(tree[2*ti+1].x,tree[2*ti+1].l));
	}

	void update(ll ti,ll l,ll r,ll left,ll right,ll x){
		if(l>right || r<left) return;
		if(l>=left && r<=right){
			tree[ti].l = min(tree[ti].l,x);
			tree[ti].x = min(tree[ti].x,x);
			return;
		}

		propagate(ti);

		ll mid = (l+r)/2;
		update(2*ti,l,mid,left,right,x);
		update(2*ti+1,mid+1,r,left,right,x);

		up(ti);

	}

	ll query(ll ti,ll l,ll r,ll left,ll right){
		if(l>right || r<left) return 1e18;
		if(l>=left && r<=right){
			return min(tree[ti].x,tree[ti].l);
		}

		propagate(ti);

		ll mid = (l+r)/2;
		ll t1 = query(2*ti,l,mid,left,right);
		ll t2 = query(2*ti+1,mid+1,r,left,right);

		up(ti);

		return min(t1,t2);
	}

	void update(ll left,ll right,ll x){
		update(1,0,max_n-1,left,right,x);
	}
	ll query(ll left,ll right){
		return query(1,0,max_n-1,left,right);
	}

};

struct Edge
{
	ll l,r,c;
	Edge() {}
	Edge(ll l,ll r,ll c) : l(l) , r(r) , c(c) {}
};

ll n,m;
Edge e[MAX];

int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin>>n>>m;
	For(i,0,m){
		cin>>e[i].l>>e[i].r>>e[i].c;
		e[i].l--;
		e[i].r--;
	}
	sort(e,e+m,[](const Edge& a,const Edge& b)->bool{
		return a.r>b.r;
	});

	if(e[0].r!=n-1){
		cout<<-1<<endl;
		return 0;
	}

	SegmentTree dp(n);
	dp.update(n-1,n-1,0);

	For(i,0,m){
		ll mini = dp.query(e[i].l,e[i].r);
		dp.update(e[i].l,e[i].r,mini+e[i].c);

		// For(i,0,n){
		// 	cout<<dp.query(i,i)<<" ";
		// }
		// cout<<endl;

	}

	if(dp.query(0,0)==1e18){
		cout<<-1<<endl;
	}else{
		cout<<dp.query(0,0)<<endl;
	}































	
}

Measured memory_bytes: 8704