#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll inf = 1e14;

vector<pii> ve[maxn];
ll dis[maxn];
priority_queue<pii,vector<pii>,greater<pii> > heap;
int N,M;

void init(){
	for(int i = 1;i<=N;i++) dis[i] = inf;
	for(int i = 1;i<=N;i++) ve[i].push_back({0,i-1});
	
}

ll Dji(){
	heap.push({0,1});
	dis[1] = 0;
	
	while(heap.size()){
		auto cur = heap.top();heap.pop();
		ll d = cur.first,id = cur.second;
		for(auto v:ve[id]){
			ll w = v.first,id2 = v.second;
			if(d+w<dis[id2]){
				dis[id2] = d+w;
				heap.push({dis[id2],id2});
			}
		}
	}
	if(dis[N] == inf) return -1;
	return dis[N];
}

int main(){
	cin>>N>>M;
	init();
	int a,b,c;
	for(int i = 0;i<M;i++){
		scanf("%d%d%d",&a,&b,&c);
		ve[a].push_back({c,b});
	}
	cout<<Dji()<<endl;
	return 0;
}