#include<iostream>
#include<vector>
#include<string.h>
#include<queue>
using namespace std;
const long long N = 4e6 + 7;
struct node {
	long long to, cost;
}r;
struct que {
	long long x, y;
	bool friend operator < (que a, que b) {
		return a.x > b.x;
	}
}w;
long long vis[N];
long long dis[N];
vector<node>v[N];
long long n, m, x, y, z;
void push(long long x, long long y, long long z) {
	r.to = y; r.cost = z;
	v[x].push_back(r);
}
long long inf = 214722121483647;
priority_queue<que> q;
long long slove(long long x) {
	fill(dis, dis + N, inf);
	w.x = 0; w.y = 1;
	dis[1] = 0;
	q.push(w);
	while (!q.empty()) {
		w = q.top(); q.pop();
		long long pos = w.y;
		for (long long i = 0; i < v[pos].size(); i++) {
			node e = v[pos][i];
			if (dis[e.to] > dis[pos] + e.cost) {
				dis[e.to] = dis[pos] + e.cost;
				w.x = dis[e.to]; w.y = e.to;
				q.push(w);
			}
		}
		
	}
	return dis[n];
}
int main() {
	cin >> n >> m;
	for (long long i = 1; i <= n; i++) {
		push(i, i - 1, 0);
	}
	while (m--) {
		cin >> x >> y >> z;
		push(x, y, z);
	}
	if (slove(1) == inf) cout << "-1\n";
	else cout << dis[n] << '\n';
	return 0;
}
