//#pragma GCC target("avx2")
#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

template<class T>
struct LazySegTree {
    using OP = function<T(T, T)>;  // function type for operation
    using UP = function<void(T &, T)>; // function type for updater

    OP op;
    UP up_d, up_l;
    T ue_d, ue_l; // unit element for dat and lazy
    int N;
    vector<T> dat, lazy;

    int get_n(int n) {
        int ret = 1;
        while (ret < n) ret <<= 1;

        return ret;
    }

    explicit LazySegTree(int n, const OP _op, const UP _up_d, const UP _up_l,
                         const T _ue_d, const T _ue_l)
            : op(_op), up_d(_up_d), up_l(_up_l), ue_d(_ue_d), ue_l(_ue_l) {
        N = get_n(n);
        dat.assign(N * 2 - 1, ue_d);
        lazy.assign(N * 2 - 1, ue_l);
    }

    /* set, a is 0-indexed */
    void init(const T& v) {
        REP(i, N) dat[i + N - 1] = v;
        for(int i = N - 2; i >= 0; --i) {
            dat[i] = op(dat[i*2+1], dat[i*2+2]);
        }
    }

    inline void evaluate(int k) {
        if (lazy[k] == ue_l) return;

        if (k < N - 1) {
            up_l(lazy[k * 2 + 1], lazy[k]);
            up_l(lazy[k * 2 + 2], lazy[k]);
        }

        up_d(dat[k], lazy[k]);
        lazy[k] = ue_l;
    }

    // update [a, b). 0-indexed!!!!!!!!!!!
    void update(int a, int b, const T &v, int k = 0, int l = 0, int r = -1) {
        evaluate(k);
        if (r < 0) r = N;

        if (r <= a || b <= l) return;

        if (a <= l && r <= b) {
            up_l(lazy[k], v);
            evaluate(k);
            return;
        }

        int mid = (l + r) / 2;
        update(a, b, v, k * 2 + 1, l, mid);
        update(a, b, v, k * 2 + 2, mid, r);

        dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
    }

    // update single position
    void update(int i, const T &v) {
        update(i, i+1, v);
    }

    // query for [a, b). 0-indexed!!!!!!!!!!!
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = N;

        if (r <= a || b <= l) return ue_d;

        evaluate(k);
        if (a <= l && r <= b) return dat[k];

        int mid = (l + r) / 2;
        T q1 = query(a, b, k * 2 + 1, l, mid);
        T q2 = query(a, b, k * 2 + 2, mid, r);

        return op(q1, q2);
    }

    void debug_() {
#ifdef LOCAL
        int c = 1;

        /* dat */
        cout << "### dat ###" << endl;

        REP(i, dat.size()) {
            if (i == c) {
                cout << endl;
                c = (c << 1) + 1;
            }
            cout << dat[i] << " ";
        }
        cout << endl;

        c = 1;
        /* lazy */
        cout << "### lazy ###" << endl;

        REP(i, lazy.size()) {
            if (i == c) {
                cout << endl;
                c = (c << 1) + 1;
            }
            cout << lazy[i] << " ";
        }

        cout << endl;
#endif
    }
};

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    
    int n, m;
    cin >> n >> m;

    vector<pair<int, pair<int, ll>>> lrc(m);
    REP(i, m) {
        int l, r; ll c;
        cin >> l >> r >> c;
        l--;

        lrc[i] = make_pair(l, make_pair(r, c));
    }

    sort(ALL(lrc));

    auto op = [](ll a, ll b) { return min(a, b);};
    auto up_d = [](ll &d, ll v) { d = min(d, v); };
    auto up_l = [](ll &l, ll v) { l = min(l, v); };
    LazySegTree<ll> st(n, op, up_d, up_l, INF, INF);
    st.update(0, 0);

    REP(i, m) {
        auto &p = lrc[i];
        int l, r; ll c;
        l = p.fi, r = p.se.fi, c = p.se.se;

        ll min_v = st.query(l, r);

        if (min_v >= INF) {
            print(-1);
            return 0;
        }

        st.update(l, r, min_v + c);
    }

     print(st.query(n - 1, n) >= INF ? -1 : st.query(n - 1, n));
}


Measured memory_bytes: 9476