#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

#define int long long
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define itrep(i, a) for (auto i = (a).begin(); i != (a).end(); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair((a), (b))

using namespace std;

int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };

template<class T> void inputVector(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}

struct Info {
    int L, R, C;

    Info(int L, int R, int C) : L(L), R(R), C(C) {}

    bool operator< (const Info &a) const {
        if (L == a.L && R == a.R) return C < a.C;
        if (L == a.L) return R < a.R;
        return L < a.L;
    }
};

int dp[100010];

class RangeMinQuery {
private:
    int bottomSt, bottomEd;
    vector<int> a;
    vector<int> rs, re;
    vector<int> cand;
    int set(int n) {
        int left = 2 * n + 1;
        int right = 2 * n + 2;
        if (left <= a.size() - 1) {
            a[n] = min(set(left), set(right));
            rs[n] = rs[left];
            re[n] = re[right];
        }
        return a[n];
    }
    void search(int s, int t, int n) {
        if (s <= rs[n] && re[n] <= t) {
            cand.push_back(a[n]);
            return;
        }
        int left = 2 * n + 1;
        int right = 2 * n + 2;
        if (s <= re[left]) search(s, t, left);
        if (rs[right] <= t) search(s, t, right);
    }
public:
    RangeMinQuery(vector<int> &v) {
        bottomEd = 0;
        for (int i = 1, plus = 1; plus < v.size(); i++) {
            plus *= 2;
            bottomEd += plus;
        }
        a.resize(bottomEd + 1);
        rs.resize(bottomEd + 1);
        re.resize(bottomEd + 1);

        bottomSt = bottomEd / 2;

        for (int i = bottomSt; i <= bottomEd; i++) {
            int j = i - bottomEd / 2;
            a[i] = j < v.size() ? v[j] : LLONG_MAX;
            rs[i] = j;
            re[i] = j;
        }

        set(0);
    }

    void update(int index, int x) {
        int pt = bottomSt + index;
        a[pt] = x;
        while (pt > 0) {
            pt = (pt - 1) / 2;
            a[pt] = min(a[2 * pt + 1], a[2 * pt + 2]);
        }
    }

    int find(int s, int t) {
        cand.clear();
        search(s, t, 0);
        int ret = cand[0];
        for (int i = 1; i < cand.size(); i++) {
            ret = min(ret, cand[i]);
        }
        return ret;
    }
};

signed main() {
    int N, M;
    cin >> N >> M;

    vector<Info> infos;
    rep(i, M) {
        int L, R, C;
        cin >> L >> R >> C;
        L--; R--;
        infos.push_back(Info(L, R, C));
    }

    sort(all(infos));

    const int INF = LLONG_MAX / 10;
    rep(i, N) dp[i] = INF;
    dp[0] = 0;

    vector<int> v(N, INF);
    RangeMinQuery rmq(v);
    rmq.update(0, 0);

    for (auto &info : infos) {
        int minval = rmq.find(info.L, N - 1);
        if (minval == INF) {
            cout << -1 << endl;
            return 0;
        }
        dp[info.L] = minval;
        if (dp[info.R] > dp[info.L] + info.C) {
            dp[info.R] = dp[info.L] + info.C;
            rmq.update(info.R, dp[info.R]);
        }
    }

    if (dp[N - 1] == INF) {
        cout << -1 << endl;
    } else {
        cout << dp[N - 1] << endl;
    }

    return 0;
}


Measured memory_bytes: 12272