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

const int MAXN = 100005;

int N, M, Li, Ri, Ci;
vector < pair <int, int> > evs[MAXN];

int main(){
	scanf("%d %d", &N, &M);
	while(M--){
		scanf("%d %d %d", &Li, &Ri, &Ci);
		Li--, Ri--;
		
		evs[Li].push_back(make_pair(Ri, Ci));
	}
	
	set < pair <long long, int> > S;
	S.insert(make_pair(0, 0));
	
	for(int i = 0; i < N; i++){
		while(!S.empty() && S.begin()->second < i)
			S.erase(S.begin());
			
		if(S.empty()){
			printf("-1\n");
			return 0;
		}
		
		long long dist = S.begin()->first;
		
		if(i == N - 1){
			printf("%lld\n", dist);
			return 0;
		}
		
		for(pair <int, int> e : evs[i])
			S.insert(make_pair(dist + e.second, e.first));
	}
	
	return 0;
}
