#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

long long N, M, L[1 << 18], R[1 << 18], C[1 << 18];
long long dist[1 << 18];
vector<pair<int, int>> X[1 << 18];
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> Q;

int main() {
	cin >> N >> M;
	for (int i = 1; i <= M; i++) cin >> L[i] >> R[i] >> C[i];
	for (int i = 1; i <= M; i++) X[L[i]].push_back(make_pair(R[i], C[i]));
	for (int i = N - 1; i >= 1; i--) X[i + 1].push_back(make_pair(i, 0));
	for (int i = 1; i <= N; i++) dist[i] = (1LL << 60);

	Q.push(make_pair(0, 1)); dist[1] = 0;
	while (!Q.empty()) {
		int pos = Q.top().second; Q.pop();
		for (pair<int, int> i : X[pos]) {
			int to = i.first;
			long long cost = i.second;
			if (dist[to] > dist[pos] + cost) {
				dist[to] = dist[pos] + cost;
				Q.push(make_pair(dist[to], to));
			}
		}
	}

	if (dist[N] == (1LL << 60)) cout << "-1" << endl;
	else cout << dist[N] << endl;
	return 0;
}