#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>

#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define clr(ar, val) memset(ar, val, sizeof(ar))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll,int> P;
typedef long double ld;
const ll MAX_D = 10000000000000000LL;

int main(void)
{
	int n, m;
	scanf("%d %d", &n, &m);
	vector<pair<int, int> > graph[n];
	ll d[n] = {};
	repp (i, 1, n) {
		d[i] = MAX_D;
	}
	rep (i, m) {
		int l, r, c;
		scanf("%d %d %d", &l, &r, &c);
		graph[l - 1].push_back(make_pair(r - 1, c));
	}
	repp (i, 1, n) {
		graph[i].push_back(make_pair(i - 1, 0));
	}
	priority_queue<P> que;
	que.push(make_pair(0LL, 0));
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		// cout << p.second << " " << p.first << endl;
		if (d[v] < p.first) continue;
		for (auto e : graph[v]) {
			// cout << x.first << " " << d[x.first] << endl;
			if (d[e.first] > d[v] + e.second) {
				d[e.first] = d[v] + e.second;
				que.push(make_pair(d[e.first], e.first));
			}
		}
	}
	printf("%d\n", (d[n - 1] >= MAX_D ? -1 : d[n - 1]));
	return 0;
}