#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}

template<typename T> vector<T> make_vector(size_t sz){
	return vector<T>(sz);
}

template<typename T,typename... Ts> 
auto make_vector(size_t sz, Ts... ts){
	return vector<decltype(make_vector<T>(ts...))>(sz, make_vector<T>(ts...));
}

template<typename T,typename U,typename... V> 
typename enable_if<is_same<T, U>::value!=0>::type 
fill_value(U &u, const V... v){
	u=U(v...);
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_value(U &u, const V... v){
	for(auto &e:u){
		fill_value<T>(e,v...);
	}
}

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

const pid_t pid = getpid();
// Problem Specific Parameter:
#define error(args...) { vector<string> _debug = split(#args, ',');err(begin(_debug), args);}

vector<string> split(const string& s, char c){
	vector<string> v;stringstream ss(s);string x;
	while (getline(ss, x, c)) v.emplace_back(x);
	return move(v);
}

void err(vector<string>::iterator it) {cerr << endl;}
template<typename T, typename... Args> void err(vector<string>::iterator it, T a,Args... args){
	cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << " ",err(++it, args...);
}


using edge = struct{int to; ll cost;};
const int ndummy = 1 << 17;
const int nmax = 2 * ndummy;
const int limit = 5 * ndummy;
vector<edge> graph[limit];

using range = tuple<int, int, int>;
vector<range> stock;


void helper(int a, int b, ll c){
	// cerr << a << " " << b <<  " " << c << endl;
	graph[a].push_back({b, c});
}

void init(int k, int l, int r) {
	const int len = r - l;
	if (len <= 1) return;
	
	const int m = (l + r) / 2;

	helper(2 * k + 1, k, 0);
	helper(k + nmax, 2 * k + 1 + nmax, 0);

	helper(2 * k + 2, k, 0);
	helper(k + nmax, 2 * k + 2 + nmax, 0);

	init(2 * k + 1, l, m);
	init(2 * k + 2, m, r);
}


void get_range(int a, int b, int k, int l, int r) {
	const int len = r - l;	
	if (r <= a or b <= l) return;

	if (a <= l and r <= b) {
		stock.push_back(range(l, r, k));
		return;
	}

	const int m = (l + r) / 2;
	get_range(a, b, 2 * k + 1, l, m);
	get_range(a, b, 2 * k + 2, m, r);
}

void add_edge(int l, int r, ll c){
	stock.clear();
	get_range(l, r, 0, 0, ndummy);
	sort(begin(stock), end(stock));
	const int m = stock.size();
	
	rep(i, m) rep(j, i, m){
		const int ik = get<2>(stock[i]);
		const int jk = get<2>(stock[j]);
		helper(ik, jk + nmax, c);
	}
}

const ll inf = 1LL << 60;
auto dijkstra(int n, int s) {
    vector<ll> dist(n, inf);
    using state = tuple<ll, int>;

    priority_queue<state, vector<state>, greater<state>> q;
    dist[s] = 0, q.push(state(0, s));

    while (!q.empty()) {
        ll c; int v;
        tie(c, v) = q.top(); q.pop();
        for (auto &e : graph[v]) {
            ll nc = c + e.cost; int nv = e.to;
            if (chmin(dist[nv], nc)) q.push(state(nc, nv));
        }
    }
    return dist;
}

int main(void){
	int n, m;
	cin >> n >> m;

	init(0, 0, ndummy);

	rep(i, m){
		int l, r, c;
		cin >> l >> r >> c;
		l--;
		add_edge(l, r, c);
	}

	const int offset = 2 * nmax;
	const int all = offset + n;
	const int base = ndummy  - 1;

	rep(i, n){
		helper(i + offset, i + base, 0);
		helper(i + base + nmax, i + offset, 0);
	}

	auto dist = dijkstra(all, offset);

	if(dist[n - 1 + offset] == inf){
		cout << -1 << endl;
	} else {
		cout << dist[n - 1 + offset] << endl;
	}

	return 0;
}

Measured memory_bytes: 267812