#if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <array>
#include <string>
#include <tuple>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>
#include <cstdint>

/* ----- Type Alias ----- */
using lint = long long int;
using ldouble = long double;
template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T>
T ndvec(T v) { return v; }

template <class T, class... Ts>
auto ndvec(size_t l, Ts... ts) {
    return std::vector<decltype(ndvec<T>(ts...))>(l, ndvec<T>(ts...));
}

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
using Edges = std::vector<Edge<Cost>>;
template <class Cost = int>
using Graph = std::vector<std::vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

struct Fout {
    int precision;
    explicit Fout(int precision) : precision(precision) {}
};

std::ostream& operator<<(std::ostream& os, const Fout& fio) {
    os << std::fixed << std::setprecision(fio.precision);
    return os;
}

/* ----- Constants ----- */
constexpr lint INF = std::numeric_limits<lint>::max() / 3;
// constexpr int MOD = 1000000007;
// const Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

template <class Data, class Operator>
class SegmentTree {
    using DataMerger = std::function<Data(Data, Data)>;
    using Applier = std::function<Data(Operator, Data)>;

private:
    int length;
    std::vector<Data> dat;
    Data dat_id;        // データの単位元
    DataMerger dmerge;  // データをマージする関数
    Applier app;        // データに作用素を適用する関数

    Data query(int ql, int qr, int nidx, int nl, int nr) {
        if (nr <= ql || qr <= nl) return dat_id;
        if (ql <= nl && nr <= qr) return dat[nidx];
        Data vl = query(ql, qr, nidx * 2, nl, (nl + nr) / 2);
        Data vr = query(ql, qr, nidx * 2 + 1, (nl + nr) / 2, nr);
        return dmerge(vl, vr);
    }

public:
    explicit SegmentTree(int N, Data dat_id, DataMerger dmerge, Applier app)
        : length(1), dat_id(dat_id), dmerge(dmerge), app(app) {
        while (length < N) length <<= 1;
        dat.assign(length * 2, dat_id);
    }

    // half-open interval [ql, qr)
    Data query(int ql, int qr) { return query(ql, qr, 1, 0, length); }

    void update(int idx, Operator e) {
        int nidx = idx + length;
        dat[nidx] = app(e, dat[nidx]);
        while (nidx > 0) {
            nidx >>= 1;
            dat[nidx] = dmerge(dat[nidx * 2], dat[nidx * 2 + 1]);
        }
    }
};

struct Seg {
    int l, r;
    lint c;
};

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

    std::vector<Seg> segs(m);
    for (auto& s : segs) {
        std::cin >> s.l >> s.r >> s.c;
        --s.l, --s.r;
    }

    std::sort(segs.begin(), segs.end(),
              [](Seg a, Seg b) { return a.r < b.r; });

    SegmentTree<lint, lint> seg(
        n, INF,
        [](lint a, lint b) { return std::min(a, b); },
        [](lint e, lint a) { return std::min(e, a); });
    seg.update(0, 0);

    for (auto s : segs) {
        seg.update(s.r, seg.query(s.l, s.r) + s.c);
    }

    lint ans = seg.query(n - 1, n);
    std::cout << (ans == INF ? -1 : ans) << std::endl;
    return 0;
}


Measured memory_bytes: 3840