#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#define int long long
#define mod 1000000007
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int n, m;
vector<pair<pair<int, int>, int>>v;
set<pair<int, pair<int, int>>>s;
int a[123456], p = 1;
signed main() {
	a[1] = 0;
	cin >> n >> m;
	for0(i, m) {
		pair<pair<int, int>, int>p1;
		cin >> p1.first.first >> p1.first.second >> p1.second;
		v.push_back(p1);
	}
	sort(v.begin(), v.end());
	int q = 0;
	while (p < n) {
		int q1 = q;
		while (v[q].first.first <= p && q < m) {
			v[q].second += a[v[q].first.first];
			s.insert(make_pair(v[q].second, v[q].first));
			q++;
		}
		if (s.size() == 0 && q1 == q) { cout << "-1\n"; return 0; }
		pair<int, pair<int, int>> p1 = *s.begin(); s.erase(s.begin());
		for (int j = p + 1; j <= p1.second.second; j++)a[j] = p1.first;
		p = p1.second.second;
	}
	cout << a[n] << endl;
}