#include <iostream>
#include <vector>
#include <string.h>
#include <set>
#include <map>
#include <assert.h>
#include <algorithm>
#include <queue>
#include <bitset>
#include <stack>
#include <chrono>
#include <random>
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define ll long long
#define MOD 1000000007 
#define INF 1000000000000000000
#define uid uniform_int_distribution <int>
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
// Read the question carefully and see all the given sample tests
// Think about the correctness before implementing

int main()
{
	int n, m;
	cin >> n >> m;
	vector <pair<int, pair<int, int>>> v[n + 1];
	for (int i = 0; i < m; i++)
	{
		int x, y, c;
		cin >> x >> y >> c;
		v[x].push_back({-c, {x, y}});
	}
	priority_queue <pair<int, pair<int, int>>> pq;
	for (auto g : v[1])
	{
		pq.push(g);
	}
	vector <ll> ans(n + 1, INF);
	ans[1] = 0;
	int p = 2;
	while(!pq.empty())
	{
		int cost = -pq.top().ff;
		int l = pq.top().ss.ff;
		int r = pq.top().ss.ss;
		pq.pop();
		while(p <= r)
		{
			ans[p] = ans[l] + cost;
			for (auto g : v[p])
			{ 
				pq.push(g);
			}
			p++;
		}
	}
	if(ans[n] != INF)
		cout << ans[n];
	else
		cout << -1;b
}
