#include <bits/stdc++.h>
 
#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < n; i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<ll, int> ii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<ii> vii;

const int MX = 100005;
int n, m, l[MX], r[MX], c[MX];
ll dp[MX];
vi pos[MX];
priority_queue<ii, vii, greater<ii>> pq;

int main () {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> l[i] >> r[i] >> c[i];
		pos[r[i]].pb(i);
	} 

	memset(dp, -1, sizeof(dp));
	dp[n] = 0;
	
	for (int i = n; i; i--) {
		while (pq.size() && pq.top().se > i)
			pq.pop();

		if (pq.size())
			dp[i] = pq.top().fi;
		
		if (dp[i] == -1) {
			cout << -1 << endl;
			return 0;
		}

		for (int j : pos[i])
			pq.emplace(dp[i] + c[j], l[j]);
	}

	cout << dp[1] << endl;

	return 0;
}
