//#define _GLIBCXX_DEBUG
#include "bits/stdc++.h"

using namespace std;

//------------------------------- Libraries --------------------------------//

template <class T>
class SegmentTree
{
    //private:
public:
    int n;
    const T unit = (T)1e15;
    vector<T> node;

public:
    SegmentTree(int n_)
    {
        this->n = 1;
        while (this->n < n_)
        {
            this->n *= 2;
        }
        this->node.resize(2 * n - 1, this->unit);
    }

    SegmentTree(vector<T> v)
    {
        int n_ = v.size();
        this->n = 1;
        while (this->n < n_)
        {
            this->n *= 2;
        }
        node.resize(2 * this->n - 1, this->unit);
        for (int i = 0; i < this->n; i++)
        {
            this->node[this->n - 1 + i] = v[i];
        }
        for (int i = this->n - 2; i >= 0; i--)
        {
            this->node[i] = min(this->node[i * 2 + 1], this->node[i * 2 + 2]);
        }
    }

    void update(int idx, T val)
    {
        idx += this->n - 1;
        this->node[idx] = min(val, this->node[idx]);
        while (idx)
        {
            idx = (idx - 1) / 2;
            this->node[idx] = min(this->node[2 * idx + 1], this->node[2 * idx + 2]);
        }
    }

    T getmin(int a, int b, int now = 0, int l = 0, int r = -1)
    {
        if (r < 0)
        {
            r = this->n;
        }
        if (r <= a || b <= l)
        {
            return this->unit;
        }
        if (a <= l && r <= b)
        {
            return this->node[now];
        }
        T lVal = getmin(a, b, 2 * now + 1, l, (l + r) / 2);
        T rVal = getmin(a, b, 2 * now + 2, (l + r) / 2, r);
        return min(lVal, rVal);
    }
};
//------------------------------- Type Names -------------------------------//

using i64 = int_fast64_t;

using seika = string;
//akari : 1D, yukari : 2D, maki : 3D vector
template <class kizuna>
using akari = vector<kizuna>;
template <class yuzuki>
using yukari = akari<akari<yuzuki>>;
template <class tsurumaki>
using maki = akari<yukari<tsurumaki>>;
//akane : ascending order, aoi : decending order
template <class kotonoha>
using akane = priority_queue<kotonoha, akari<kotonoha>, greater<kotonoha>>;
template <class kotonoha>
using aoi = priority_queue<kotonoha>;

//------------------------------- Dubug Functions ---------------------------//
inline void print()
{
    cout << endl;
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest)
{
    cout << first << ' ';
    print(rest...);
}
//------------------------------- Solver ------------------------------------//

void solve()
{
    int n, m;
    cin >> n >> m;
    akari<tuple<int, int, i64>> ps(m);
    for (int i = 0; i < m; i++)
    {
        int l, r;
        i64 c;
        cin >> l >> r >> c;
        l--, r--;
        ps[i] = make_tuple(l, r, c);
    }
    sort(ps.begin(), ps.end());
    vector<long> v(n, (1LL << 60));
    v[0] = 0;
    SegmentTree<long> segtree(v);
    for (auto &p : ps)
    {
        int l, r;
        i64 c;
        tie(l, r, c) = p;
        i64 mini = segtree.getmin(l, r + 1);
        segtree.update(r, mini + c);
        //print(l, r, c, mini);
    }
    i64 ans = segtree.getmin(n - 1, n);
    cout << (ans == 1e15 ? -1 : ans) << endl;
}

int main()
{
    solve();
    return 0;
}

Measured memory_bytes: 5376