#include <bits/stdc++.h>

using namespace std;

constexpr int N = 100000;
constexpr int M = 100000;

set<pair<long long, int>> active;
vector<int> e_open[N + 1];
vector<int> e_close[N + 1];
int l[M + 1];
int r[M + 1];
long long c[M + 1];
long long dist[N + 1];
long long comp[N + 1];

int main() {
	int n, m, p, i, j;

	scanf("%d%d", &n, &m);

	for (i = 1; i <= m; i++) {
		scanf("%d%d%lld", l + i, r + i, c + i);
		e_open[l[i]].push_back(i);
		e_close[r[i]].push_back(i);
	}

	dist[1] = 0;

	// Pushing initial ranges.
	for (j = 0; j < e_open[1].size(); j++) {
		p = e_open[1][j];
		comp[p] = dist[1] + c[p];
		active.insert({comp[p], p});
	}

	for (i = 2; i <= n; i++) {
		if (active.empty()) {
			break;
		}

		dist[i] = active.begin()->first;

		for (j = 0; j < e_open[i].size(); j++) {
			p = e_open[i][j];
			comp[p] = dist[i] + c[p];
			active.insert({comp[p], p});
		}

		for (j = 0; j < e_close[i].size(); j++) {
			p = e_close[i][j];
			active.erase({comp[p], p});
		}
	}

	if (i <= n) {
		printf("-1\n");
	}
	else {
		printf("%lld\n", dist[n]);
	}

	return 0;
}