#include <iostream>
#include <vector>
#include <array>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <complex>
#include <bitset>
#include <functional>

using namespace std;
using ll=long long int;

template <class Monoid, class Operator, class Apply>
struct LazySegTree {
    using T = typename Monoid::type;
    Monoid M;
    using U = typename Operator::type;
    Operator Op;
    Apply apply;
    int n;
    vector<T> v;
    vector<U> f;

    LazySegTree(
        int n_,
        const Monoid &m = Monoid(),
        const Operator &op = Operator(),
        const Apply &ap = Apply()) 
    : M(m), Op(op), apply(ap) 
    {
        n = 1;
        while (n < n_) n *= 2;
        v.assign(n * 2, M.id());
        f.assign(n * 2, Op.id());
    }

    LazySegTree(
        int n_,
        const vector<T> &init,
        const Monoid &m = Monoid(),
        const Operator &op = Operator(),
        const Apply &ap = Apply())
    : LazySegTree(n_, m, op, ap) 
    {
        copy(init.begin(), init.end(), v.begin() + n);
        for (int k = n-1; k > 0; k--) {
            v[k] = M.append(v[2*k+0], v[2*k+1]);
        }
    }

    void propagate(int k, int l, int r) {
        range_apply(0, n, 2*k+0, l, (l+r)/2, f[k]);
        range_apply(0, n, 2*k+1, (l+r)/2, r, f[k]);
        f[k] = Op.id();
    }

    void point_set(int i, T a) {
        point_set(i, 1, 0, n, a);
    }
    void point_set(int i, int k, int l, int r, T a) {
        if (k == n + i) {
            v[k] = a;
        } else if (r <= i || i < l) {
            return;
        } else {
            propagate(k, l, r);
            point_set(i, 2*k+0, l, (l+r)/2, a);
            point_set(i, 2*k+1, (l+r)/2, r, a);
            v[k] = M.append(v[2*k+0], v[2*k+1]);
        }
    }
    void range_apply(int a, int b, U z) {
        range_apply(a, b, 1, 0, n, z);
    }
    void range_apply(int a, int b, int k, int l, int r, U z) {
        if (a <= l && r <= b) {
            v[k] = apply(v[k], z, l, r);
            f[k] = Op.append(f[k], z);
        } else if (r <= a || b <= l) {
            return;
        } else {
            propagate(k, l, r);
            range_apply(a, b, k*2+0, l, (l+r)/2, z);
            range_apply(a, b, k*2+1, (l+r)/2, r, z);
            v[k] = M.append(v[k*2+0], v[k*2+1]);
        }
    }
    T range_concat(int a, int b) {
        return range_concat(a, b, 1, 0, n);
    }
    T range_concat(int a, int b, int k, int l, int r) {
        if (a <= l && r <= b) return v[k];
        if (r <= a || b <= l) return M.id();
        propagate(k, l, r);
        T vl = range_concat(a, b, k*2+0, l, (l + r) / 2);
        T vr = range_concat(a, b, k*2+1, (l + r) / 2, r);
        return M.append(vl, vr);
    }
};

struct minmn {
    using type = ll;
    type id() {return 1e18;}
    type append(type a, type b) {return min(a, b);}
};
struct apply {
    ll operator()(ll a, ll z, int l, int r) {
        return min(a, z);
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector<pair<pair<int, int>, int>> edges(m);
    for (auto &e: edges) cin >> e.first.first >> e.first.second >> e.second;

    sort(edges.begin(), edges.end());

    LazySegTree<minmn, minmn, apply> st(n);
    st.range_apply(0, 1, 0);
    for (auto &e: edges) {
        int l, r, c;
        l = e.first.first; r = e.first.second; c = e.second;
        l--;
        st.range_apply(l, r, st.range_concat(l, l+1) + c);
    }

    ll ans = st.range_concat(n-1, n);
    if (ans == minmn().id()) ans = -1;

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 5504