#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define P pair<ll, int>
const ll INF = (1ll<<60);
ll d[100100];
vector<P> g[100100];
priority_queue<P, vector<P>, greater<P> > pq;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	int n,m,l,r;
	ll c;
	cin >> n >> m;
	for(int i = 0 ;i < 100100 ;i++)d[i]=INF;
	d[0] = 0ll;
	pq.push(P(d[0],0));
	for(int i=1; i<n; ++i){
		g[i].push_back(P(0ll,i-1));
	}
	for(int 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(int i=(g[l].size()-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));
			}
		}
	}
	if(d[n-1]<INF)cout<<d[n-1];
	else cout<<-1;
	return 0;
}
