#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std ;
using namespace  __gnu_pbds ;
using namespace __gnu_cxx ;

typedef long long ll ;
typedef long double ld ;

typedef pair<int, int> pii ;
typedef pair<ll,ll> pll ;

typedef vector<int> vi ;
typedef vector<ll> vll ;
typedef vector<pii> vpi ;
typedef vector<pll> vpl;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define per(i,b,a) for (int i = (b); i >= (a); i--)

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define fi first
#define se second

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

const int MOD1 = 1000000007 ;
const int MOD2 = 998244353 ;
const ll  INFL = 1e18 ;
const int INFI = 2e9 ;
const int MAXN = 100005 ;
const int LOGN = 30 ;
const int ALPHA = 27 ;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ;

ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; }
void upmin(int &a, int b) { if(a < b) { a = b ; } }
void relax(int &a, int b) { if(a > b) { a = b ; } }
ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return  a ; }
ll subtract(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; }
ll multiply(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; }

int n ;
int m ;
pair<int, pair<int, ll> > edges[MAXN] ;
ll stree[2*MAXN] ;
void init() {
     rep(i,n,2*n-1) {
        stree[i] = INFL ;
     }
     per(i,n-1,1) {
         stree[i] = min(stree[2*i], stree[2*i+1]) ;
     }
     stree[n] = 0 ;
}
void update(int l, int r, ll v) {
     l += n ;
     r += n ;
     while(l < r) {
        if(l&1) {
            stree[l] = min(stree[l], v) ;
            l++ ;
        }
        if(r&1) {
            r-- ;
            stree[r] = min(stree[r], v) ;
        }
        l /= 2 ;
        r /= 2 ;
     }
}
ll query(int l) {
   ll res = INFL ;
   l += n ;
   while(l) {
         res = min(res, stree[l]) ;
         l /= 2 ;
   }
   return res ;
}
int main() {
    ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    cin >> n >> m ;
    rep(i,0,m-1) {
        cin >> edges[i].fi >> edges[i].se.fi >> edges[i].se.se ;
        edges[i].fi-- ;
        edges[i].se.fi-- ;
    }
    sort(edges,edges+m) ;
    init() ;
    rep(i,0,m-1) {
        int l = edges[i].fi ;
        int r = edges[i].se.fi ;
        ll cost = edges[i].se.se ;
        ll delta = query(l) ;
        if(delta == INFL) {
            cout << "-1\n" ;
            return 0 ;
        }
        ll real_cost = cost + delta ;
        update(l, r+1, real_cost) ;
    }
    ll res = query(n-1) ;
    cout << res << endl ;
}
