#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair <ll, ll> pii;

const ll N = 1e5 + 5, mod = 1e15;

ll dist[N];
vector <pii> adj[N];
ll n, m;

void dij() {
	for (int i = 0; i < n; i++)	dist[i] = mod;
	set <pii> s;
	dist[0] = 0;
	s.insert({0, 0});
	while(!s.empty()) {
		int v = (*s.begin()).second;
		s.erase(s.begin());
		for (auto e : adj[v]) {
			int w = e.second, u = e.first;
			if(dist[u] > dist[v] + w) {
				s.erase({dist[u], u});
				dist[u] = dist[v] + w;
				s.insert({dist[u], u});
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	vector <pair <pii , int> > edge;
	for (int i = 0; i < n - 1; i++) {
		adj[i + 1].push_back({i, 0});
	}
	for (int i = 0; i < m; i++) {
		int v, u, w;
		cin >> v >> u >> w;
		v--, u--;
		adj[v].push_back({u, w});
		adj[u].push_back({v, w});
	}
	dij();
	if(dist[n - 1] == mod)	cout << -1 << "\n";
	else
		cout << dist[n - 1] << "\n";
}