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

const int maxn = 110000;
const long long inf = (long long)1e18 + 1e9;

long long C[maxn];
int N;

inline void update(int x, long long val) {
	for(; x; x -= x & -x) C[x] = min(C[x], val);
}

inline long long get(int x) {
	long long ret = inf;
	for(; x <= N; x += x & -x) ret = min(ret, C[x]);
	return ret;
}

struct Dist {
	int l, r, c;
	Dist(int l = 0, int r = 0, int c = 0) : l(l), r(r), c(c) {}
} d[maxn];

inline bool operator < (const Dist& lhs, const Dist& rhs) {
	return lhs.l < rhs.l;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m;
	N = n;
	for(int i = 1; i <= n; ++ i) C[i] = inf;
	update(1, 0);
	for(int i = 0; i < m; ++ i) {
		cin >> d[i].l >> d[i].r >> d[i].c;
	}
	sort(d, d + m);
	int j = 0;
	for(int i = 1; i <= n; ++ i) {
		for(; j < m && d[j].l == i; ++ j) {
			update(d[j].r, get(d[j].l) + d[j].c);
		}
	}
	long long ans = get(n);
	cout << (ans == inf ? -1 : ans) << "\n";
	return 0;
}
