#include <bit>
#include <bitset>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>


typedef long double ld;
typedef long long ll;

using namespace std;

#define INF (1LL << 61)

#define MAXN (400410)

ll t[MAXN];

#define N (1 << 17)

ll tt[N + N];

ll mymin(int l, int r) {
	ll ret = INF;
	l += N;
	r += N;
	while (l <= r) {
		ret = min(ret, tt[l]);
		ret = min(ret, tt[r]);
		l >>= 1;
		l++;
		r >>= 1;
		r--;
	}
	return ret;
}

ll myup(int pos, int val) {
	pos += N;
	while (pos) {
		tt[pos] = min(tt[pos], val);
		pos >>= 1;
	}
}

ll getmin(int v, int tl, int tr, int l, int r) {
	if (l > r) {
		return INF;
	}
	if ((l == tl) && (r == tr)) {
		return t[v];
	}
	int tm = (tl + tr) / 2;
	ll m1 = getmin(2 * v, tl, tm, l, min(r, tm));
	ll m2 = getmin(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
	return min(m1, m2);
}

void update(int v, int tl, int tr, int pos, ll new_val) {
	if (tl == tr) {
		t[v] = new_val;
	} else {
		int tm = (tl + tr) / 2;
		if (pos <= tm) {
			update(2 * v, tl, tm, pos, new_val);
		} else {
			update(2 * v + 1, tm + 1, tr, pos, new_val);
		}
		t[v] = min(t[2 * v], t[2 * v + 1]);
	}
}

struct edge {
	int l, r, c;

	edge(int tl = 0, int tr = 0, int tc = 0) {
		l = tl;
		r = tr;
		c = tc;
	}

	void read() {
		cin >> l >> r >> c;
		l--;
		r--;
	}

	bool operator<(const edge & e) const {
		return (l < e.l);
	}

	void print() {
		cerr << l << " " << r << " " << c << endl;
	}
};

int n, m;

edge a[MAXN];
ll ans[MAXN];

ll quickmin(int l, int r) {
	return getmin(1, 0, n - 1, l, r);
}

void quickup(int pos, ll new_val) {
	//cerr << pos << " " << new_val << endl;
	update(1, 0, n - 1, pos, new_val);
}

int main() {
	for (int i = 0; i < MAXN; i++) {
		t[i] = INF;
		ans[i] = INF;
	}
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		a[i].read();	
	}
	sort(a, a + m);
	ans[0] = 0;
	quickup(0, 0);
	//cerr << "here" << endl;
	int curedge = 0;
	for (int i = 0; i < n; i++) {
		ll & curdist = ans[i];
		curdist = quickmin(i, n - 1);
		
		if ((curedge >= m) || (a[curedge].l > i)) {
			continue;
		}
	
		while ((curedge < m) && (a[curedge].l == i)) {
	//		a[curedge].print();
			ll old = quickmin(a[curedge].r, a[curedge].r); 
			ll nnext = curdist + a[curedge].c;
			if (old > nnext) {
				quickup(a[curedge].r, nnext);	
			}
			curedge++;
		}
	}
	ll ret = ans[n - 1];
	if (ret == INF) {
		cout << -1 << endl;
	} else {
		cout << ret << endl;
	}

	return 0;
}
