#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

ll d[100100];
vector<P> g[100100];
priority_queue<P, vector<P>, greater<P> > pq;

int main(void){
	int n,m,l,r,i;
	ll c;
	fill(d,d+100100,INFLL);
	d[0] = 0ll;
	pq.push(P(d[0],0));
	cin >> n >> m;
	for(i=1; i<n; ++i){
		g[i].push_back(P(0ll,i-1));
	}
	for(i=0; i<m; ++i){
		cin >> l >> r >> c;
		--l; --r;
		g[l].push_back(P(c,r));
	}
	while(!pq.empty()){
		l = pq.top().second;
		c = pq.top().first;
		pq.pop();
		if(c>d[l]){
			continue;
		}
		for(i=(g[l].l_ength()-1); i>=0; --i){
			r = g[l][i].second;
			c = g[l][i].first;
			if(d[r] > d[l] + c){
				d[r] = d[l] + c;
				pq.push(P(d[r],r));
			}
		}
	}
	cout << ((d[n-1]<INFLL)?d[n-1]:(-1ll)) << endl;
	return 0;
}
