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

#define NDEBUG
#include <cassert>


typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<llll> vllll;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define eb  emplace_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define repC3(vari,varj,vark,n)  for(int vari=0;vari<(n)-2;++vari)for(int varj=vari+1;varj<(n)-1;++varj)for(int vark=varj+1;vark<(n);++vark)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair
#define clamp(v,lo,hi) min(max(v,lo),hi)

template <typename T>
inline bool found(set<T> &s, T elem) { return s.find(elem) != s.end(); }
template <typename T>
inline bool found(unordered_set<T> &s, T elem) { return s.find(elem) != s.end(); }
template <typename T, typename U>
inline bool found(map<T,U> &s, U elem) { return s.find(elem) != s.end(); }
template <typename T, typename U>
inline bool found(unordered_map<T,U> &s, U elem) { return s.find(elem) != s.end(); }
template <typename T>
inline bool found(vector<T> &s, T elem) { return find(s.begin(), s.end(), elem) != s.end(); }
inline int found(string &s, string t) { return (int)s.find(t); }
inline int found(string &s, int c) { return (int)s.find(c); }

template<typename T1, typename T2> inline void amin(T1 & a, T2 const & b) { if (a>b) a=b; }
template<typename T1, typename T2> inline void amax(T1 & a, T2 const & b) { if (a<b) a=b; }
template<typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template<typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

inline ll square(ll x) { return x * x; }
inline ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }
template <typename T>
inline T mod(T a, T b) { return ((a % b) + b) % b; }

template <typename T>
int find_left(vector<T>& v, T elem) {
    return (int)(upper_bound(v.begin(), v.end(), elem) - v.begin()) - 1;
}
template <typename T>
int find_right(vector<T>& v, T elem) {
    return (int)(lower_bound(v.begin(), v.end(), elem) - v.begin());
}




template <typename T, typename OP>
class LazySegmentTree {
 public:
    using MERGE = function<T(T,T)>;
    using APPLY = function<T(T,OP)>;
    using MERGE_OP = function<OP(OP,OP)>;

    MERGE merge_data_func;
    APPLY apply_lazy_func;
    MERGE_OP merge_lazy_func;
    T elem_ident;
    OP op_ident;
    std::vector<T> data;
    std::vector<OP> lazy;
    int n, height;

    LazySegmentTree(MERGE f, APPLY g, MERGE_OP h, T elem_ident, OP op_ident)
        : merge_data_func(f), apply_lazy_func(g), merge_lazy_func(h),
          elem_ident(elem_ident), op_ident(op_ident) {
    }

    void init(int n_temp) {
        n = 1; height = 0;
        while (n < n_temp) { n <<= 1; ++height; }
        data.assign(2*n, elem_ident);
        lazy.assign(2*n, op_ident);
    }

    void build(const std::vector<T> &v){
        int n_temp = v.size();
        init(n_temp);
        std::copy(v.begin(), v.end(), data.begin()+n);
        for (int i=n-1; i>0; --i) {
            data[i] = merge_data_func(data[(i << 1)|0], data[(i << 1)|1]);
        }
    }

    inline T reflect_lazy(int k){
        if (lazy[k] == op_ident) {
            return data[k];
        } else {
            T applied = apply_lazy_func(data[k], lazy[k]);
            return applied;
        }
    }

    inline void eval(int k){
        if (lazy[k] == op_ident) return;
        lazy[(k << 1)|0] = merge_lazy_func(lazy[(k << 1)|0], lazy[k]);
        lazy[(k << 1)|1] = merge_lazy_func(lazy[(k << 1)|1], lazy[k]);
        data[k] = reflect_lazy(k);
        lazy[k] = op_ident;
    }

    inline void eval_down(int k) {
        for (int i=height; i>0; --i) {
            eval(k >> i);
        }
    }

    inline void merge_up(int k) {
        while (k >>= 1) {
            data[k] = merge_data_func(reflect_lazy((k << 1)|0), reflect_lazy((k << 1)|1));
        }
    }

    void update(int a, int b, OP x) {
        eval_down(a += n);
        eval_down(b += n-1);
        for (int l=a,r=b+1; l<r; l>>=1,r>>=1) {
            if (l&1) { lazy[l] = merge_lazy_func(lazy[l], x); ++l; }
            if (r&1) { --r; lazy[r] = merge_lazy_func(lazy[r], x); }
        }
        merge_up(a);
        merge_up(b);
    }

    void set_val(int a, T x) {
        eval_down(a += n);
        data[a] = x;
        lazy[a] = op_ident;
        merge_up(a);
    }

    T query(int a, int b) {
        eval_down(a += n);
        eval_down(b += n-1);
        T vl = elem_ident, vr = elem_ident;
        for (int l=a,r=b+1; l<r; l>>=1,r>>=1) {
            if (l & 1) {
                vl = merge_data_func(vl, reflect_lazy(l++));
            }
            if (r & 1) {
                vr = merge_data_func(reflect_lazy(--r), vr);
            }
        }
        T merged = merge_data_func(vl, vr);
        return merged;
    }

    void desc() {
    }
};


ll solve(int N, int M, vi& l, vi& r, vi& c) {
    map<int, set<ii>> mp;
    rep(i,M){
        mp[ l[i] ].insert( ii(r[i], c[i]) );
    }

    auto f = [](ll a, ll b){ return min(a,b); };
    auto g = [](ll a, ll b){ return min(a,b); };
    auto h = [](ll a, ll b){ return min(a,b); };

    LazySegmentTree<ll,ll> st(f, g, h, LLONG_MAX, LLONG_MAX);

    st.build(vector<ll>(N+1, LLONG_MAX));
    st.desc();


    for(int i=1; i<N; ++i){
        ll x = (i==1) ? 0 : st.query(i, i+1);
        if (x == LLONG_MAX) return -1;

        for(ii p: mp[i]) {
            st.update(i+1, p.first+1, x + p.second);
        }
    }

    ll x = st.query(N, N+1);
    if (x == LLONG_MAX) return -1;
    return x;
}

int main() {
    int N, M; scanf("%d %d%*c", &N, &M);
    vi l(M),r(M),c(M);
    rep(i,M) scanf("%d %d %d%*c", &l[i], &r[i], &c[i]);
    cout << solve(N,M,l,r,c) << endl;
    return 0;
}


Measured memory_bytes: 19968