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

#define ll long long

const ll llf = (signed ll) (~0ull >> 3);

const int N = 1e5 + 5;

typedef class Edge {
	public:
		int L, R;
		ll c;

		void read() {
			scanf("%d%d%lld", &L, &R, &c);
		}
		boolean operator < (Edge b) const {
			return c > b.c;
		}
} Edge;

int n, m;
ll f[N];
vector<Edge> G[N];
priority_queue<Edge> Q;

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		f[i] = llf;
	f[1] = 0;
	for (int i = 1; i <= m; i++) {
		Edge tmp;
		tmp.read();
		G[tmp.L].push_back(tmp);
	}
	for (int i = 1; i <= n; i++) {
		while (!Q.empty() && Q.top().R < i)
			Q.pop();
		if (i > 1) {
			if (Q.empty()) {
				puts("-1");
				return 0;
			}
			f[i] = Q.top().c;
		}
		for (auto e : G[i]) {
			e.c += f[i];
			Q.push(e);
		}
	}
	printf("%lld\n", f[n]);
	return 0;
}
