#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 Elem, typename LazyOperand, typename MERGE_DATA_PROC, typename APPLY_LAZY_PROC, typename MERGE_LAZY_PROC>
class LazySegmentTree {
 public:
    MERGE_DATA_PROC f;
    APPLY_LAZY_PROC g;
    MERGE_LAZY_PROC h;
    Elem elem_ident;
    LazyOperand lazy_operand_ident;
    vector<Elem> elems;
    vector<LazyOperand> lazy_operands;
    int n, height;

    inline void assign_merged_lazy(LazyOperand& dest, LazyOperand x) {
        dest = h(dest, x);
    }

    LazySegmentTree(MERGE_DATA_PROC f, APPLY_LAZY_PROC g, MERGE_LAZY_PROC h,
                    Elem elem_ident, LazyOperand lazy_operand_ident)
        : f(f), g(g), h(h), elem_ident(elem_ident), lazy_operand_ident(lazy_operand_ident) {
    }

    void init(int n_temp) {
        n = 1; height = 0;
        while (n < n_temp) { n <<= 1; ++height; }
        elems.assign(2*n, elem_ident);
        lazy_operands.assign(2*n, lazy_operand_ident);
    }

    void build(const vector<Elem>& v){
        int n_temp = v.size();
        init(n_temp);
        rep(i,n_temp) elems[n+i] = v[i];
        for (int i=n-1; i>0; --i) {
            elems[i] = f(elems[i*2], elems[i*2+1]);
        }
    }

    inline Elem reflect(int k){
        return (lazy_operands[k] == lazy_operand_ident) ? elems[k] : g(elems[k], lazy_operands[k]);
    }

    inline void _eval(int k){
        if (lazy_operands[k] != lazy_operand_ident) {
            assign_merged_lazy(lazy_operands[k*2], lazy_operands[k]);
            assign_merged_lazy(lazy_operands[k*2+1], lazy_operands[k]);

            elems[k] = reflect(k);
            lazy_operands[k] = lazy_operand_ident;
        }
    }

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

    inline void recalc(int k) {
        while (k >>= 1) {
            elems[k] = f(reflect(k*2), reflect(k*2+1));
        }
    }

    void update(int a, int b, LazyOperand x) {
        a += n; b += n;
        eval_down(a);
        eval_down(b-1);
        for (int l=a,r=b; l<r; l>>=1,r>>=1) {
            if (l & 1) assign_merged_lazy(lazy_operands[l++], x);
            if (r & 1) assign_merged_lazy(lazy_operands[--r], x);
        }
        recalc(a);
        recalc(b-1);
    }

    void set_val(int a, Elem e) {
        a += n;
        eval_down(a);
        elems[a]         = e;
        lazy_operands[a] = lazy_operand_ident;
        recalc(a);
    }

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

    void desc() {
    }
};

/***
    using ll2 = pair<ll, int>;
    auto f_sum = [](ll2 a, ll2 b){ return ll2(a.first + b.first, a.second + b.second); };
    auto g = [](ll2 a, ll b){
        ll p = a.second * b;
        return ll2(a.first + p, a.second); };
    auto h = [](ll a, ll b){ return a+b; };
    LazySegmentTree<ll2,ll,decltype(f_sum),decltype(g),decltype(h)> st(f_sum, g, h, ll2(0,0), 0);
    st.build(vector<ll2>(N, ll2(0, 1)));

    auto g = [](ll2 a, ll b){
        return ll2((a.second % 2) ? a.first^b : a.first, a.second); }
    auto h = [](ll a, ll b){ return a^b; };
    LazySegmentTree<ll,ll,decltype(f_sum),decltype(g),decltype(h)> st(f_sum, g, h, ll2(0,0), 0);
    st.build(vector<ll>(N, ll2(0,1));

    auto g = [](ll2 a, ll b){
         ll p = a.second * b;
         return ll2(p ? p-a.first : a.first, a.second); }
    auto h = [](ll a, ll b){ return a^b; };
    LazySegmentTree<ll,ll,decltype(f_sum),decltype(g),decltype(h)> st(f_sum, g, h, ll2(0,0), 0);
    st.build(vector<ll>(N, ll2(0,1));

    auto f = [](ll a, ll b){ return min(a,b); }
    auto g = [](ll a, ll b){ return b; }
    auto h = [](ll a, ll b){ return min(a,b); };
    LazySegmentTree<ll,ll,decltype(f),decltype(g),decltype(h)> st(f, g, h, LLONG_MAX, LLONG_MAX);
    st.build(vector<ll>(N, LLONG_MAX));

    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,decltype(f),decltype(g),decltype(h)> st(f, g, h, LLONG_MAX, LLONG_MAX);
    st.build(vector<ll>(N, LLONG_MAX));
***/


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 b; };
    auto h = [](ll a, ll b){ return min(a,b); };

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

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

    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