#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;
using pll = pair<ll, ll>;

ll const INF = ll(1e18);

struct Seq {
    int l;
    int r;
    ll c;
};

bool operator<(Seq const& lhs, Seq const& rhs) {
    if (lhs.l == rhs.l) {
        if (lhs.r == rhs.r) {
            return lhs.c < rhs.c;
        }
        return lhs.r < rhs.r;
    }
    return lhs.l < rhs.l;
}

struct RNGT {
    int nd;
    vector<ll> d;

    RNGT(int n) {
        nd = n;
        d.resize(nd * 4, INF);
    }

    void update(int qp, int val) {
        return __update(1, 0, nd-1, qp, val);
    }
    void __update(int p, int l, int r, int qp, int val) {
        if (qp<l || r<qp) return;
        if (l==r) {
            d[p] = min(d[p], val);
            return;
        }

        int mid = l + (r - l) / 2;
        __update(p * 2, l, mid, qp, val);
        __update(p * 2 + 1, mid + 1, r, qp, val);
        d[p] = min(d[p*2], d[p*2+1]);
    }

    ll query(int ql, int qr) {
        return __query(1, 0, nd-1, ql, qr);
    }
    ll __query(int p, int l, int r, int ql, int qr) {
        if (r<ql || qr<l) return INF;
        if (l == r) return d[p];

        int mid = l + (r-l)/2;
        return min(__query(p*2, l, mid, ql, qr), __query(p*2+1, mid+1, r, ql, qr));
    }
};

int main() {
    int n, m;
    cin >> n >> m;

    vector<Seq> vs;
    int l, r, c;
    for (int i = 0; i < m; ++i) {
        cin >> l >> r >> c;
        --l, --r;
        vs.push_back({l, r, c});
    }
    sort(vs.begin(), vs.end());

    RNGT segt(n);
    segt.update(0, 0);
    for (int i=0; i<m; ++i) {
        segt.update(vs[i].r, segt.query(vs[i].l, vs[i].r-1) + vs[i].c);
    }

    ll ans = segt.query(n - 1, n - 1);
    cout << (ans == INF ? -1 : ans) << endl;
}