/*

Author : AllKilled
Date   : 201911096

Contest: NIKKEI Programming Contest 2019-2.
Problem: Task D.
Result :

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

typedef long long LL;
typedef pair<LL,LL> PLL;

#define lson ro<<1
#define rson ro<<1|1

const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const LL MAXN = 1e5+7;

///--------------------------------------------///

struct SegmentTree{
    LL n, t[MAXN<<2], p[MAXN<<2], mk[MAXN<<2];
    SegmentTree(LL m = 0){
        n = m;
    }

    void pushUp(LL ro){
        if(t[rson] < t[lson]){
            t[ro] = t[rson];
            p[ro] = p[rson];
        }
        else {
            t[ro] = t[lson];
            p[ro] = p[lson];
        }
    }

    void upd(LL ro, LL val){
        t[ro] = min(t[ro], val);
        if(mk[ro]) mk[ro] = min(mk[ro], val);
        else mk[ro] = val;
    }

    void pushDown(LL ro){
        upd(lson, mk[ro]);
        upd(rson, mk[ro]);
        mk[ro] = 0;
    }

    void build(LL ro, LL tl, LL tr){
        t[ro] = 0;
        p[ro] = 0;
        if(tl == tr){
            t[ro] = INF;
            p[ro] = tl;
            mk[ro] = 0;
            return ;
        }
        LL mid = tl + tr >> 1;
        build(lson, tl, mid);
        build(rson, mid+1, tr);
        pushUp(ro);
    }

    void update(LL ro, LL tl, LL tr, LL ql, LL qr, LL val){
        if(ql <= tl && tr <= qr){
            upd(ro, val);
            return ;
        }
        LL mid = tl + tr >> 1;
        if(mk[ro]) pushDown(ro);
        if(ql <= mid) update(lson, tl, mid, ql, qr, val);
        if(qr > mid) update(rson, mid+1, tr, ql, qr, val);
        pushUp(ro);
    }

    PLL query(LL ro, LL tl, LL tr, LL ql, LL qr){
        if(ql <= tl && tr <= qr){
            return {t[ro], p[ro]};
        }
        LL mid = tl + tr >> 1;
        if(mk[ro]) pushDown(ro);
        PLL res, left = {INF, INF}, right = {INF, INF};
        if(ql <= mid) left = query(lson, tl, mid, ql, qr);
        if(qr > mid) right = query(rson, mid+1, tr, ql, qr);
        if(right.first < left.first) res = right;
        else res = left;
        return res;
    }

    void debug(){
        cout << "---------------" << endl;

        for(int i = 1; i <= n; ++i){
            PLL res = query(1, 1,n, i,i);
            cout << res.first <<" ";
        } cout << endl;

        cout << "+++++++++++++++" << endl;

    }

};

LL n, m;
struct Node {
    LL l, r, c;
};
vector<Node> g;
SegmentTree st;

int main()
{
    cin >> n >> m;
    for(LL l, r, c, i = 0; i < m; ++i){
        cin >> l >> r >> c;
        g.push_back({l, r, c});
    }
    sort(g.begin(), g.end(), [](Node a, Node b){
        return (a.l < b.l) || (a.l == b.l && a.c < b.c);
    });

    st.n = n;
    st.build(1, 1, n);
    LL l = 1, r = 1;
    st.update(1, 1, n, 1, 1, 0);
    for(LL i = 0; i < m; ++i){
        if(g[i].l > r) {
            cout << "-1" <<endl;
            return 0;
        }
        PLL u = st.query(1, 1,n, g[i].l, r);
        st.update(1, 1,n, u.second+1, g[i].r, u.first+g[i].c);
        r = max(g[i].r, r);

        //cout<<endl<<i<<" : "<<u.first<<" "<<u.second<<endl;
        //cout<<g[i].l<<" "<<g[i].r<<" "<<g[i].c<<endl;
        //st.debug();
    }
    PLL res = st.query(1, 1,n, n,n);
    if(res.first == INF) cout << "-1\n";
    else cout << res.first << endl;

    return 0;
}


Measured memory_bytes: 11632