#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> P;
constexpr auto INF = INT_MAX >> 1;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {

	int n, m;
	cin >> n >> m;

	V<V<P>> e(n, V<P>(0));
	rep(i, n - 1)e[i + 1].push_back(make_pair(0, i));
	
	rep(i, m) {
		int l, r;
		ll c;
		cin >> l >> r >> c;
		l--;r--;
		e[l].push_back(make_pair(c, r));
	}

	V<ll> dist(n, LINF);dist[0] = 0;
	priority_queue<P, V<P>, greater<P>> pq;
	pq.push(make_pair(0, 0));
	
	while (!pq.empty()) {
		ll d = pq.top().first;
		int v = pq.top().second;pq.pop();
		if (dist[v] < d)continue;
		rep(i, e[v].size()) {
			int vv = e[v][i].second;
			ll dd = e[v][i].first;
			if (dist[vv] <= d + dd)continue;
			dist[vv] = d + dd;
			pq.push(make_pair(dist[vv], vv));
		}
	}

	if (dist[n - 1] == LINF)cout << -1 << endl;
	else cout << dist[n - 1] << endl;

}