#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<random>
#include<functional>
#include<utility>

typedef long long ll;

#define rep(i,n) for(int i=0; i<n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;


const ll INF = 1e18;

struct Range
{
	int l, r;
	ll c;
	Range(int l, int r, int c) :l(l), r(r), c(c) {}
	bool operator<(const Range &ra)const {
		if (l < ra.l) return true;
		if (l == ra.l) {
			if (c < ra.c) return true;
			if (c == ra.c) {
				return r > ra.r;
			}
		}
		return false;
	}
};

int N, M;
vector<Range> Ran;
ll cost[100005];
int main() {
	cin >> N >> M;
	//scanf("%d%d", &N, &M);
	rep(i, M) {
		int l, r, c;
		//scanf("%d%d%d", &l, &r, &c);
		cin >> l >> r >> c; 
		l--; r--;
		Ran.emplace_back(l, r, c);
	}
	sort(Ran.begin(), Ran.end());

	rep(i, N) cost[i] = INF;
	cost[0] = 0;
	priority_queue<PLL, vector<PLL>, greater<>> pq;
	pq.emplace(0, 0);
	while (!pq.empty())
	{
		PLL now = pq.top(); pq.pop();
		ll co = now.first, v = now.second;
		if (cost[v] < co) continue;
		auto it1 = lower_bound(Ran.begin(), Ran.end(), Range(v, 0, 0));
		if (it1 == Ran.end()) continue;
		Range nxt = *it1;
		int to = v + 1;
		while (nxt.l == v)
		{
			int nr = nxt.r; ll nco = nxt.c;
			for (; to <= nr; to++) {
				if (cost[to] > co + nco) {
					cost[to] = co + nco;
					pq.emplace(cost[to], to);
				}
			}
			it1++;
			if (it1 == Ran.end()) break;
			nxt = *it1;
			if (nxt.r <= nr) break;
		}
	}

	ll ans = cost[N - 1];
	if (ans == INF) ans = -1;
	cout << ans << endl;
	//printf("%lld\n", ans);

	return 0;
}
