#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define MOD @
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

int N, M;
int L[101010], R[101010], C[101010];
vector<pair<int, i64>> graph[(1 << 19) + 101010];
const int AUG = 1 << 17;
const int AUG2 = 1 << 18;
i64 mind[1010101];
bool visited[1010101];

void ae(int p, int q, i64 d)
{
	graph[p].push_back(make_pair(q, d));
}

const i64 INFV = 1LL << 60LL;

struct segtree
{
	static const int DEPTH = 18;
	static const int N = 1 << DEPTH;

	i64 delay[2 * N], minv[2 * N];
	int activ[2 * N];

	segtree() {
		for (int i = 0; i < 2 * N; ++i) delay[i] = minv[i] = INFV;
		for (int i = N; i < 2 * N; ++i) activ[i] = 1;
		for (int i = N - 1; i >= 1; ++i) activ[i] = activ[i * 2] + activ[i * 2 + 1];
	}

	void update(int p)
	{
		i64 ll = activ[p * 2] == 0 ? INFV : minv[p * 2];
		i64 rr = activ[p * 2 + 1] == 0 ? INFV : minv[p * 2 + 1];
		minv[p] = min(ll, rr);
	}
	void upd_range(int p, int qL, int qR, int dep, i64 val)
	{
		int pL = p << dep;
		int pR = (p + 1) << dep;
		if (pR <= qL || qR <= pL) return;
		if (qL <= pL && pR <= qR) {
			delay[p] = minv[p] = val;
			return;
		}
		if (delay[p] != -1) {
			delay[p * 2] = delay[p * 2 + 1] = delay[p];
			delay[p] = -1;
		}
		upd_range(p * 2, qL, qR, dep - 1, val);
		upd_range(p * 2 + 1, qL, qR, dep - 1, val);
		update(p);
		minv[p] = min(minv[p * 2], minv[p * 2 + 1]);
	}
	void upd_range(int qL, int qR, i64 val)
	{
		upd_range(1, qL + N, qR + N, DEPTH, val);
	}
	void deactivate(int p)
	{
		p += N;
		--activ[p];
		p >>= 1;
		while (p) {
			--activ[p];
			update(p);
			p >>= 1;
		}
	}
	int find_best()
	{
		int p = 1;
		for (int i = 0; i < DEPTH; ++i) {
			if (activ[p * 2] == 0) p = p * 2 + 1;
			else if (activ[p * 2 + 1] == 0) p = p * 2;
			else if (minv[p] < minv[p * 2]) p = p * 2;
			else p = p * 2 + 1;
		}
		return p - N;
	}
};

int main()
{
	scanf("%d%d", &N, &M);
	for (int i = 0; i < M; ++i) {
		scanf("%d%d%d", L + i, R + i, C + i);
		--L[i];
	}
	for (int i = 2; i < AUG2; ++i) {
		ae(i, i / 2, 0);
	}
	for (int i = 2; i < AUG2; ++i) {
		ae(i / 2 + AUG2, i + AUG2, 0);
	}
	for (int i = 1; i < AUG2; ++i) {
		ae(i + AUG2, i, 0);
	}
	for (int i = 0; i < M; ++i) {
		int l = L[i] + AUG; int r = R[i] + AUG;
		int pp = AUG2 * 2 + i;
		while (l < r) {
			if (l & 1) {
				ae(l, pp, C[i]);
				ae(pp, l + AUG2, 0);
				++l;
			}
			if (r & 1) {
				--r;
				ae(r, pp, C[i]);
				ae(pp, r + AUG2, 0);
			}
			l >>= 1; r >>= 1;
		}
	}
	for (int i = 0; i < M + 2 * AUG2; ++i) {
		mind[i] = INFV;
		visited[i] = false;
	}
	priority_queue<pair<i64, int>> qu;
	qu.push({0LL, AUG});
	while (!qu.empty()) {
		auto top = qu.top(); qu.pop();
		int p = top.second;
		if (visited[p]) continue;
		visited[p] = true;
		i64 d = -top.first;
		//printf("%d %lld\n", p, d);

		if (p == N - 1 + AUG) {
			printf("%lld\n", d);
			return 0;
		}

		for (auto e : graph[p]) {
			int q = e.first;
			i64 d2 = d + e.second;
			qu.push({-d2, q});
		}
	}

	puts("-1");

	return 0;
}


Measured memory_bytes: 140108