#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(long long i=0;i<x;i++)
#define repn(i,x) for(long long i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
vector<pair<string,P> >vec;
// vector<vector<int>> data(3, vector<int>(4));


const ll MAX_N = 1 << 20;
// セグメント木を持つグローバル配列 
ll n, dat[2 * MAX_N - 1];
// 初期化
void init(ll n_) {
	// 簡単のため、要素数を2のべき乗に 
	n = 1;
	while (n < n_) n *= 2;
	// すべての値をINT_MAXに
	for (ll i = 0; i < 2 * n - 1; i++) dat[i] = INT_MAX; 
}
// k番目の値(0-indexed)をaに変更
void update(ll k, ll a) {
	// 葉の節点
	k += n - 1; 
	dat[k] = a; 
	// 登りながら更新
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}

ll query(ll a, ll b, ll k, ll l, ll r) {
	// [a, b)と[l, r)が交差しなければ、INT_MAX 
	if (r <= a || b <= l) return INT_MAX;
	// [a, b)が[l, r)を完全に含んでいれば、この節点の値 
	if (a <= l && r <= b) return dat[k]; 
	else {
		// そうでなければ、2つの子の最小値
		ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2); 
		ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	} 
}



vector<ll> dp(100001, INT_MAX);

ll main(){	
	ll M;
	ll N;
	scanf("%d",&N);
	init(N);
	scanf("%d",&M);
	vector<ll> C(M-1+1);
	vector<ll> L(M-1+1);
	vector<ll> R(M-1+1);
	vector<vector<ll>> lv(M, vector<ll>(3));
	for(ll i = 0 ; i <= M-1 ; i++){
		scanf("%d",&L[i]);
		scanf("%d",&R[i]);
		scanf("%d",&C[i]);
		L[i]--; R[i]--;
		lv[i][0] = L[i];
		lv[i][1] = R[i];
		lv[i][2] = C[i];
	}
	dp[0] = 0;
	update(0, 0);
	sort(lv.begin(),lv.end());
	// rep(i, M) {
	// 	cout << lv[i][0] << " " << lv[i][1] << " " << lv[i][2] << endl;
	// }
	rep(i, M) {
		ll l = lv[i][0], r = lv[i][1], c = lv[i][2];
		// cout << lv[i][0] << " " << lv[i][1] << " " << lv[i][2] << endl;
		
		dp[r] = min(dp[r], query(l, r+1, 0, 0, n)+c);
		update(r, dp[r]);

		// rep(j, N+1) if (dp[j] >= INT_MAX/4) cout << "i"; else cout << dp[j];
		// cout << endl;
	}

	if (dp[N-1] >= INT_MAX ) printf("-1\n"); else printf("%d\n",dp[N-1]);
	return 0;
}

