#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
#define REP(i, n) for(int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct edge { int to; int cost; };
vector<edge> G[200000];
vector<ll> d(200000, INF);
void dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first) continue;
		REP(i,G[v].size()) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main() {
	int n, m; cin >> n >> m;
	REP(i, n) G[i + 1].push_back({ i,0 });
	int a, b, c;
	REP(i, m) {
		cin >> a >> b >> c;
		G[a].push_back({ b,c });
	}
	dijkstra(1);
	if (d[n] == INF)
		cout << -1;
	else
		cout << d[n] << endl;
	return 0;
}