#include <bits/stdc++.h>
using namespace std;

#define rep(i, j) for (ll i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vll;
typedef vector<pii> vpii;

const ll inf = (1LL << 50);

#include <functional>
#include <vector>

// Thanks drken1215
// http://drken1215.hatenablog.com/entry/2019/02/19/110200



// Segment Tree
template<class Monoid, class Action> struct SegTree {
    using FuncMonoid = function< Monoid(Monoid, Monoid) >;
    using FuncAction = function< void(Monoid&, Action) >;
    using FuncLazy = function< void(Action&, Action) >;
    FuncMonoid FM;
    FuncAction FA;
    FuncLazy FL;
    Monoid UNITY_MONOID;
    Action UNITY_LAZY;
    ll SIZE, HEIGHT;
    vector<Monoid> dat;
    vector<Action> lazy;
    
    SegTree() { }
    SegTree(ll n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
            const Monoid &unity_monoid, const Action &unity_lazy)
    : FM(fm), FA(fa), FL(fl), UNITY_MONOID(unity_monoid), UNITY_LAZY(unity_lazy) {
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }
    void init(ll n, const FuncMonoid fm, const FuncAction fa, const FuncLazy fl,
              const Monoid &unity_monoid, const Action &unity_lazy) {
        FM = fm; FA = fa; FL = fl;
        UNITY_MONOID = unity_monoid; UNITY_LAZY = unity_lazy;
        SIZE = 1; HEIGHT = 0;
        while (SIZE < n) SIZE <<= 1, ++HEIGHT;
        dat.assign(SIZE * 2, UNITY_MONOID);
        lazy.assign(SIZE * 2, UNITY_LAZY);
    }
    
    /* set, a is 0-indexed */
    void set(ll a, const Monoid &v) { dat[a + SIZE] = v; }
    void build() {
        for (ll k = SIZE - 1; k > 0; --k)
            dat[k] = FM(dat[k*2], dat[k*2+1]);
    }
    
    /* update [a, b) */
    inline void evaluate(ll k) {
        if (lazy[k] == UNITY_LAZY) return;
        if (k < SIZE) FL(lazy[k*2], lazy[k]), FL(lazy[k*2+1], lazy[k]);
        FA(dat[k], lazy[k]);
        lazy[k] = UNITY_LAZY;
    }
    inline void update(ll a, ll b, const Action &v, ll k, ll l, ll r) {
        evaluate(k);
        if (a <= l && r <= b)  FL(lazy[k], v), evaluate(k);
        else if (a < r && l < b) {
            update(a, b, v, k*2, l, (l+r)>>1), update(a, b, v, k*2+1, (l+r)>>1, r);
            dat[k] = FM(dat[k*2], dat[k*2+1]);
        }
    }
    inline void update(ll a, ll b, const Action &v) { update(a, b, v, 1, 0, SIZE); }
    
    /* get [a, b) */
    inline Monoid get(ll a, ll b, ll k, ll l, ll r) {
        evaluate(k);
        if (a <= l && r <= b)
            return dat[k];
        else if (a < r && l < b)
            return FM(get(a, b, k*2, l, (l+r)>>1), get(a, b, k*2+1, (l+r)>>1, r));
        else
            return UNITY_MONOID;
    }
    inline Monoid get(ll a, ll b) { return get(a, b, 1, 0, SIZE); }
    inline Monoid operator [] (ll a) { return get(a, a+1); }
    
    /* debug */
    void prll() {
        for (ll i = 0; i < SIZE; ++i) { cout << (*this)[i]; if (i != SIZE) cout << ","; }
        cout << endl;
    }
};



int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    ll n, m;
    cin >> n >> m;
    vll l(m), r(m), c(m);
    vector<tuple<ll, ll, ll>> v;

    rep(i, m) {
        ll l, r, c;
        cin >> l >> r >> c;
        l--, r--;
        v.emplace_back(l, r, c);
    }

    sort(all(v), [](const tuple<ll,ll,ll>& a, const tuple<ll,ll,ll>& b) {
        ll al, bl, ar, br, ac, bc;
        tie(al, ar, ac) = a;
        tie(bl, br, bc) = b;
        if(al != bl) return al < bl;
        else return ac < bc;
    });

/*
    rep(i, m) {
        ll l, r, c;
        tie(l,r,c) = v[i];
        cout << l << " " << r << " " << c << endl;
    }
*/

    auto fm = [](ll a, ll b) {return min(a, b);};
    auto fa = [](ll &a, ll d) {a = min(a, d);};
    auto fl = [](ll &d, ll e) {d = min(d, e);};

    SegTree<ll, ll> seg(n + 1, fm, fa, fl, inf, inf);
    seg.set(0, 0);
    for (ll i = 1; i < n; ++i) seg.set(i, inf);
    seg.build();

    for(auto& t: v) {
        ll l,r,c;
        tie(l,r,c) = t;
        ll d = seg.get(l, l+1);
        if(d == inf) continue;
        // cout << "update: " << l + 1 << " " << r + 1 << d + c << endl;
        seg.update(l+1, r+1, d + c);
    }

    ll ans = seg.get(n-1, n);
    if (ans == inf) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}


Measured memory_bytes: 9712