#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define lint long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define SZ(v) ((int)v.size())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define POPCNT(n) (__builtin_popcount(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
template <typename T> inline T LB(vector<T>& v, T a) { return *lower_bound(ALL(v),a); }
template <typename T> inline int LBP(vector<T>& v, T a) { return lower_bound(ALL(v),a) - v.begin(); }
template <typename T> inline T UB(vector<T>& v, T a) { return *upper_bound(ALL(v),a); }
template <typename T> inline int UBP(vector<T>& v, T a) { return upper_bound(ALL(v),a) - v.begin(); }
template <typename T1, typename T2> ostream& operator<< (ostream& os, const pair<T1,T2>& p) { os << p.first << " " << p.second; return os; }
template <typename T1, typename T2> istream& operator>> (istream& is, pair<T1,T2>& p) { is >> p.first >> p.second; return is; }
template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) { REP(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template <typename T> istream& operator>> (istream& is, vector<T>& v) { for(T& in : v) is >> in; return is; }
template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); }
template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; }
template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e,v); }
const lint MOD = 1000000007;
const lint INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;

template <class M>
class SegmentTree {
public:
    using F = function<M(M, M)>;
	
    int n, lev;
    F f;
    M u1;
    vector<M> d;

    SegmentTree(int n_, F f, M u1) : f(f), u1(u1)
    {
        for (n = 1, lev = 0; n < n_; n <<= 1, ++lev);
        d.assign(n << 1, u1);
    }

    SegmentTree(const vector<M> &v, F f, M u1) : f(f), u1(u1)
    {
        int n_ = v.size();
        for (n = 1, lev = 0; n < n_; n <<= 1, ++lev);
        d.assign(n << 1, u1);
        for (int i = 0; i < n_; ++i) d[n + i] = v[i];
        for (int i = n - 1; i > 0; --i) d[i] = f(d[(i << 1) | 0], d[(i << 1) | 1]);
    }

    // k番目の要素をxに更新 (k: 0-indexed)
    void update(int k, M x)
    {
        d[k += n] = x;
        while (k >>= 1) d[k] = f(d[(k << 1) | 0], d[(k << 1) | 1]);
    }

    // 半開区間 [a, b) に対するクエリ (a, b: 0-indexed)
    M query(int a, int b)
    {
        M L = u1, R = u1;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = f(L, d[l++]);
            if (r & 1) R = f(d[--r], R);
		}
        return f(L, R);
    }
    M query(int a) { return query(a, a + 1); }

    void dump(int time= -1)
    {
        if (~time) cout << "time = " << time << endl;
        for (int i = 0; i <= lev; ++i) {
            int m = 1 << i;
            for (int j = 0; j < m; ++j) {
                cout << "  " << d[j + m];
            }
            cout << endl;
        }
        cout << endl;
    }
};
template <class M = int> using ST = SegmentTree<M>;

// Range Minimum Query (RmQ)
template <class M = int>
struct RmQ : SegmentTree<M> {
    RmQ(int n, M u1 = numeric_limits<M>::max()) : SegmentTree<M>(n, [&](M a, M b) { return min(a, b); }, u1) {}
    RmQ(const vector<M> &v, M u1 = numeric_limits<M>::max()) : SegmentTree<M>(v, [&](M a, M b) { return min(a, b); }, u1) {}
};

int N, M;
int L[100010], R[100010], C[100010];
using P = tuple<int, int, int>;
vector<P> ps;

void _main() {
    cin >> N >> M;
    REP(i, M) {
        cin >> L[i] >> R[i] >> C[i];
        L[i]--; R[i]--;
        ps.emplace_back(R[i], L[i], C[i]);
    }
    SORT(ps);
    vector<int> dp(N + 1, INF);
    dp[0] = 0;
    RmQ<int> rmq(N + 1, INF);
    rmq.update(0, 0);
    REP(i, M) {
        int l, r, c;
        tie(r, l, c) = ps[i];
        int val = rmq.query(l, r);
        CHMIN(dp[r], val + c);
        rmq.update(r, dp[r]);
    }
    cout << (dp[N - 1] == INF ? -1 : dp[N - 1]) << endl;
}

signed main(signed argc, char **argv) {
    if (argc > 1) {
        if (strchr(argv[1], 'i')) freopen("input.txt", "r", stdin);
        if (strchr(argv[1], 'o')) freopen("output.txt", "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
    _main();
    return 0;
}

Measured memory_bytes: 8432