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

// macros
#define overload_rep(_1, _2, _3, name, ...) name
#define re_p(i, n) for(int i = 0; i < (n); ++i)
#define r_ep(i, x, y) for(int i = (x); i <= (y); ++i)
#define rep(...) overload_rep(__VA_ARGS__, r_ep, re_p)(__VA_ARGS__)
#define all(a) a.begin(), a.end()
#ifndef STOP_DEBUG
#define debug(...)\
    do { cerr << boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_cerr(__VA_ARGS__); cerr << noboolalpha; } while (0)
#else
#define debug(...)
#endif

// functions
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
void debug_cerr(){ cerr << '\n'; }
template<class Head, class... Tail>
void debug_cerr(Head head, Tail... tail){
    cerr << ' ' << head;
    debug_cerr(tail...);
}

// cin/cout setup
struct IoSetup{
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} iosetup;

// libraries
template<class Monoid, class Effector>
struct LazySegmentTree{
private:

    int sz, height;
    vector<Monoid> node;
    vector<Effector> lazy;
    using Func_M = function<Monoid(Monoid, Monoid)>;
    using Func_E = function<Effector(Effector, Effector)>;
    using Merge = function<Monoid(Monoid, Effector, int)>;
    const Func_M operate_m;
    const Func_E operate_e;
    const Merge merge;
    const Monoid unit_m;
    const Effector unit_e;

    inline Monoid reflect(int i, int l){
        return lazy[i] == unit_e ? node[i] : merge(node[i], lazy[i], l);
    }

    inline void propagate(int i, int l){
        if(lazy[i] == unit_e)return;
        if(i < sz){
            lazy[i << 1] = operate_e(lazy[i << 1], lazy[i]);
            lazy[i << 1 | 1] = operate_e(lazy[i << 1 | 1], lazy[i]);
        }
        node[i] = merge(node[i], lazy[i], l);
        lazy[i] = unit_e;
    }

public:

    LazySegmentTree(){}
    LazySegmentTree(const Monoid m, const Effector e, const Func_M f1, const Func_E f2, const Merge f3) 
        : unit_m(m), unit_e(e), operate_m(f1), operate_e(f2), merge(f3) {}
    LazySegmentTree(const vector<Monoid>& vec, const Monoid m, const Effector e, const Func_M f1, const Func_E f2, const Merge f3) 
        : unit_m(m), unit_e(e), operate_m(f1), operate_e(f2), merge(f3) { bulid(vec); }
    ~LazySegmentTree(){}

    inline void init(){
        fill(node.begin(), node.end(), unit_m);
        fill(lazy.begin(), lazy.end(), unit_e);
    }

    inline void bulid(const vector<Monoid>& vec){
        for(sz = 1, height = 0; sz < (int)vec.size(); sz <<= 1, ++height);
        node.assign(sz << 1, unit_m);
        lazy.assign(sz << 1, unit_e);
        for(int i = 0; i < (int)vec.size(); ++i)
            node[i + sz] = vec[i];
        for(int i = sz - 1; i > 0; --i)
            node[i] = operate_m(node[i << 1], node[i << 1 | 1]);
    }

    inline void effect(int l, int r, Effector v){
        l += sz, r += sz - 1;
        for(int i = height; i >= 0; --i){
            propagate(l >> i, 1 << i);
            propagate(r >> i, 1 << i);
        }
        for(int tl = l, tr = r + 1, i = 1; tl < tr; tl >>= 1, tr >>= 1, i <<= 1){
            if(tl & 1)propagate(tl, i), lazy[tl] = operate_e(lazy[tl], v), propagate(tl++, i);
            if(tr & 1)propagate(--tr, i), lazy[tr] = operate_e(lazy[tr], v), propagate(tr, i);
        }
        for(int tl = l >> 1, tr = r >> 1, i = 0; tl > 0; tl >>= 1, tr >>= 1, ++i){
            node[tl] = operate_m(reflect(tl << 1, 1 << i), reflect(tl << 1 | 1, 1 << i));
            node[tr] = operate_m(reflect(tr << 1, 1 << i), reflect(tr << 1 | 1, 1 << i));
        }
    }

    inline Monoid fold(int l, int r){
        Monoid vl = unit_m, vr = unit_m;
        l += sz, r += sz - 1;
        for(int i = height; i >= 0; --i){
            propagate(l >> i, 1 << i);
            propagate(r >> i, 1 << i);
        }
        for(int tl = l, tr = r + 1, i = 1; tl < tr; tl >>= 1, tr >>= 1, i <<= 1){
            if(tl & 1)vl = operate_m(vl, reflect(tl++, i));
            if(tr & 1)vr = operate_m(reflect(--tr, i), vr);
        }
        return operate_m(vl, vr);
    }

    inline Monoid operator [] (int i){
        return fold(i, i + 1);
    }

};

// declarations

// solve
int main(){

    int N, M;
    cin >> N >> M;
    vector<tuple<int, int, int>> vec(M);
    rep(i, M){
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        vec[i] = make_tuple(a, b, c);
    }

    vector<long long> dist(N, 1ll << 60);
    dist[0] = 0;
    auto f1 = [](long long x, long long y){ return min(x, y); };
    auto f2 = [](long long x, long long y, int l){ return min(x, y); };

    LazySegmentTree<long long, long long> seg(dist, 1ll << 60, 1ll << 60, f1, f1, f2);
    sort(all(vec));
    rep(i, M){
        int l, r, c;
        tie(l, r, c) = vec[i];
        long long v = seg[l] + c;
        seg.effect(l, r, v);
    }

    long long ans = seg[N - 1];
    cout << (ans == (1ll << 60) ? -1 : ans) << '\n';

}


Measured memory_bytes: 6280