#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<time.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(),(a).end()
#define EPS (1e-5)
#define bit(n,k) ((n>>k)&1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
	ll x, y;
	bool operator<(const H& b) const {
		if (x != b.x) return x < b.x;
		return y < b.y;
	}
	bool operator>(const H& b) const {
		if (x != b.x) return x > b.x;
		return y > b.y;
	}
	bool operator==(const H& b) const { return x == b.x && y == b.y; }
	bool operator!=(const H& b) const { return (*this) != b; }
};
struct P {
	ll pos, cost;
	bool operator<(const P& b) const { return cost < b.cost; }
	bool operator>(const P& b) const { return cost > b.cost; }
};
struct B {
	ll to, cost;
};
struct E {
	ll from, to, cost;
	bool operator<(const E& b) const { return cost < b.cost; }
	bool operator>(const E& b) const { return cost > b.cost; }
};
template<typename T, typename U>
void chmin(T& a, U b) {
	if (a > b) a = b;
}
template<typename T, typename U>
void chmax(T& a, U b) {
	if (a < b) a = b;
}
template<typename T>
T max_0(T a) {
	if (a < 0) return 0;
	return a;
}
template<typename T>
T min_0(T a) {
	if (a > 0) return 0;
	return a;
}
ll read() {
	ll u;
	ll k = scanf("%lld", &u);
	return u;
}
ll gcd(ll i, ll j) {
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0;
	for (int i = 0; i < 20; i++) 
		if ((1ll << i) & x) sum++;
	return sum;
}
ll factorial_[1000000];
ll comb(ll x, ll y, ll mod) {
	if (!factorial_[0]) {
		factorial_[0] = 1;
		for (int i = 1; i < 1000000; i++) {
			factorial_[i] = factorial_[i - 1] * i % mod;
		}
	}
	if (x < y) return 0;
	return mod_pow(factorial_[x], mod, mod) *
		mod_pow(factorial_[x - y], mod - 2, mod) % mod *
		mod_pow(factorial_[y], mod - 2, mod) % mod;
}//xCy
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
int n, m;
struct A {
	int l, r, c;
	bool operator<(A a)const { return l < a.l; }
};
A a[500000];
H dat[1000000];
void eval(int i, int l, int r) {
	dat[i].x = min(dat[i].x, dat[i].y);
	if (r - l > 1) {
		dat[i * 2 + 1].y = min(dat[i * 2 + 1].y, dat[i].y);
		dat[i * 2 + 2].y = min(dat[i * 2 + 2].y, dat[i].y);
	}
	dat[i].y = Inf;
}
void update(int i, int l, int r, int a, int b, int x) {
	eval(i, l, r);
	if (b <= l || r <= a) return;
	if (a <= l && r <= b) {
		dat[i].y = min(dat[i].y, x);
		eval(i, l, r);
		return;
	}
	update(i * 2 + 1, l, (l + r) / 2, a, b, x);
	update(i * 2 + 2, (l + r) / 2, r, a, b, x);
	dat[i].x = min(dat[i * 2 + 1].x, dat[i * 2 + 2].x);
}
int query(int i, int l, int r, int a, int b) {
	eval(i, l, r);
	if (b <= l || r <= a) return Inf;
	if (a <= l && r <= b) {
		return dat[i].x;
	}
	return min(query(i * 2 + 1, l, (l + r) / 2, a, b),
		query(i * 2 + 2, (l + r) / 2, r, a, b));
}
signed main() {
	cin >> n >> m;
	for (int i = 0; i < 1000000; i++) {
		dat[i].x = Inf;
		dat[i].y = Inf;
	}
	for (int i = 0; i < m; i++) {
		cin >> a[i].l >> a[i].r >> a[i].c;
		a[i].l--; a[i].r--;
	}
	sort(a, a + m);
	int r = 1;
	while (r < n) r *= 2;
	update(0, 0, r, 0, 1, 0);
	for (int i = 0; i < m; i++) {
		update(0, 0, r, a[i].l, a[i].r + 1, query(0, 0, r, a[i].l, a[i].r + 1) + a[i].c);
	}
	int k = query(0, 0, r, n - 1, n);
	if (k == Inf) cout << -1 << endl;
	else cout << k << endl;
}

Measured memory_bytes: 20864