// clang-format off
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifndef bhartiya
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
		tree_order_statistics_node_update> oset;
// not that imp
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<pii> vii;

#define INF 4557430888798830399ll
#define MOD 1000000007
#define EPS 1e-7
#define PI 3.1415926535897932385
// #define PI acos(-1)

#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()
#define sz(x) (int)(x).size()

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar))  // 0x3f for inf, 0x80 for -INF can also use with pairs

#ifdef bhartiya
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
	cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
	int bracket=0,i=0;
	for(; ;i++)
		if(names[i]==','&&bracket==0)
			break;
		else if(names[i]=='(')
			bracket++;
		else if(names[i]==')')
			bracket--;
	const char *comma=names+i;
	cout.write(names,comma-names)<<" : ";
	__p(arg1);
	cout<<"| ";
	__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define endl '\n'
#define trace(...)
#define end_routine()
#endif

mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e) {
	ll ans=1; for(;e;b=b*b%MOD,e/=2) if(e&1) ans=ans*b%MOD; return ans; }
// clang-format on

const int maxn = 1<<20; //set maxn a power of 2
 
int seg[2*maxn];
int lazy[2*maxn];

//we will always first update lazy and then change the seg by push..
//This means that if lazy is set, it's value is not yet added.
//This is very imp to understand how to initialize the values of lazy ans seg.
void push(int ind, int L, int R) {
    seg[ind] = min(seg[ind], lazy[ind]);
    if (L != R){
        lazy[2*ind] = min(lazy[ind], lazy[2*ind]);
        lazy[2*ind+1] = min(lazy[ind], lazy[2*ind+1]);
    }
    lazy[ind] = INF;
}


int combine(int val1, int val2){
    return min(val1, val2);
}

void pull(int ind) {
    seg[ind] = combine(seg[ind*2], seg[ind*2+1]);
}

//Note that when we use size as power of 2, maxn+i contains the value at node i
void build() { // initialize the values from maxn+i :p
    repr(i,maxn) pull(i);
}

int query(int lo, int hi, int ind = 1, int L = 0, int R = maxn-1) {
    push(ind,L,R);
    if (lo > R || L > hi) return INF; /////Identity element
    if (lo <= L && R <= hi) return seg[ind];
    
    int M = (L+R)/2;
    return combine(query(lo,hi,2*ind,L,M), query(lo,hi,2*ind+1,M+1,R));
}

void upd(int lo, int hi, int val, int ind = 1, int L = 0, int R = maxn-1) {
    push(ind,L,R);
    if (hi < L || R < lo) return;
    if (lo <= L && R <= hi) {
        ///// change lazy according to val
        lazy[ind] = min(lazy[ind], val);
        //////
        push(ind,L,R);
        return;
    }
    
    int M = (L+R)/2;
    upd(lo,hi,val,2*ind,L,M); upd(lo,hi,val,2*ind+1,M+1,R);
    pull(ind);
}


void solve() 
{
	int n; cin>>n;
	int m; cin>>m;
	// for(int i=0;i<=n;i++){
	// 	seg[maxn+i] = INF;
	// }
	fil(seg, 0x3f);
	fil(lazy, 0x3f);
	// build();
	upd(1,1,0);
	vector<piii> queries;
	rep(i,m){
		int l,r,c;
		cin>>l>>r>>c;
		queries.pb({{l,r},c});
	}
	sort(all(queries));
	for(auto que: queries){
		int l = que.ff.ff, r = que.ff.ss, c = que.ss;
		int val = query(l,l);
		int lo = val + c;
		upd(l,r,lo);
	}
	int ans = query(n,n);
	if(ans == INF) cout<<-1<<endl;
	else cout<<ans<<endl;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0), cout.precision(10);  // cout<<fixed;
#ifdef bhartiya
    cin.exceptions(cin.failbit);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t=1;
    // cin>>t; 
    fr(i,1,t){
        // cout<<"Case #"<<i<<": ";
        solve();
    }
    end_routine();
}

Measured memory_bytes: 37748