#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
template<typename T> class segtree {
private:
    int n,sz;
    vector<T> node, min_val, second_val, lazy;
    vector<int> count;
    vector<bool> lazyFlag;
    void update(int id){
        node[id] = node[2*id+1] + node[2*id+2];
        if(min_val[2*id+1] < min_val[2*id+2]){
            min_val[id] = min_val[2*id+1];
            second_val[id] = min(second_val[2*id+1], min_val[2*id+2]);
            count[id] = count[2*id+1];
        }else if(min_val[2*id+1] > min_val[2*id+2]){
            min_val[id] = min_val[2*id+2];
            second_val[id] = min(min_val[2*id+1], second_val[2*id+2]);
            count[id] = count[2*id+2];
        }else{
            min_val[id] = min_val[2*id+1];
            second_val[id] = min(second_val[2*id+1], second_val[2*id+2]);
            count[id] = count[2*id+1] + count[2*id+2];
        }
    }
public:
    segtree(const vector<T>& v) : n(1), sz((int)v.size()){
        while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1, false);
        min_val.resize(2*n-1, numeric_limits<T>::max());
        second_val.resize(2*n-1, numeric_limits<T>::max());
        count.resize(2*n-1, 1);
        for(int i = 0; i < sz; i++){
            node[i+n-1] = min_val[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            update(i);
        }
    }
    void eval(int k, int l, int r) {
        if(lazyFlag[k]){
            if(lazy[k] > min_val[k]){
                node[k] += (lazy[k] - min_val[k]) * count[k];
                min_val[k] = lazy[k];
                if(r - l > 1){
                    lazy[k*2+1] = lazy[k*2+2] = lazy[k];
                    lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
                }
            }
            lazyFlag[k] = false;
        }
    }
    void range(int a, int b, T x, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a || min_val[k] >= x){
            return;
        }
        if(a <= l && r <= b && second_val[k] > x) {
            lazy[k] = x;
            lazyFlag[k] = true;
            eval(k, l, r);
        }else{
            range(a, b, x, 2*k+1, l, (l+r)/2);
            range(a, b, x, 2*k+2, (l+r)/2, r);
            update(k);
        }
    }
    T query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return 0;
        }
        if(a <= l && r <= b){
            return node[k];
        }
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
    void print()
    {
        for(int i = 0; i < sz; i++){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    using P = pair<pii, ll>;
    vector<P> a(m);
    vector<vector<pair<int, ll>>> d(n);
    rep(i,m){
        int l, r;
        ll c;
        cin >> l >> r >> c;
        l--;r--;
        d[r].emplace_back(l, c);
        a[i] = P{pii(l, r), c};
    }
    for(int i = 0; i < n; i++){
        sort(d[i].begin(), d[i].end());
    }
    vector<ll> dp(n, 1LL<<60);
    vector<ll> hoge(n,-(1LL<<60));
    hoge[0] = 0;
    segtree<ll> seg(hoge);
    dp[0] = 0;
    for(int r = 1; r < n; r++){
        ll temp = 1LL<<60;
        for(auto x : d[r]){
            int l = x.first;
            ll cost = x.second;
            temp = min(temp, -seg.query(l,l+1) + cost);
        }
        seg.range(0,r+1,-temp);
    }
    ll ans = -seg.query(n-1,n);
    if(ans == (1LL<<60))ans = -1;
    cout << ans << endl;
}


Measured memory_bytes: 19968