#pragma GCC optimize("O3")
#pragma GCC target("avx")

#include <iostream>
#include <memory>
#include <algorithm>
#include <vector>
#include <utility>
#include <cassert>
#include <tuple>

// < 双対セグメント木クラス >
// @ 概要 | 区間更新・点取得が高速に行える O(logN)
// @ 要件 | monoid_type : 1, using value_type = oo; の形が記述されている等、用いる型にアクセス可能なこと
//                       2, staticメンバ変数として単位元 identity_element が定義されていること
//                       3, static関数 operation が定義されており, かつ以下の条件を満たすこと
//                           (i) (f*g)(x) = f(g(x))
template <class monoid_type> class dualSegmentTree{
private:
    using element_t = typename monoid_type::value_type;   // 要素の型 (渡した型に依存する)
    const unsigned int original_length;                   // 元の配列の長さ : A.size()
    const unsigned int height;                            // 配列の要素の数の指数部 : k
    const unsigned int length;                            // 配列の要素の数 : 2^k (2のべき乗)
    std::allocator<element_t> alloc;                      // データのアロケータ
    element_t* data;                                      // 格納しているデータ(配列)本体(を指すポインタ)

    static unsigned int set_log_len(const unsigned int n){
        unsigned int res = 0; while((1U << res) < n) ++res; return res;
    }
    inline void propagate(unsigned int idx){
        for(unsigned int i = height, jp, jc; i ; --i) if(data[jp] != monoid_type::identity_element){
            jp = idx >> i; jc = jp << 1;
            data[jc | 0] = monoid_type::operation(data[jc | 0], data[jp]);
            data[jc | 1] = monoid_type::operation(data[jc | 1], data[jp]);
            data[jp    ] = monoid_type::identity_element;
        }
    }
public:
    dualSegmentTree(const unsigned int n): original_length(n), height(set_log_len(n)), length(1U << height){
        data = alloc.allocate(length << 1); std::fill(data, data + (length << 1), monoid_type::identity_element);
    }
    template <class InputIterator>
    dualSegmentTree(InputIterator first, InputIterator last):original_length(last - first), height(set_log_len(original_length)), length(1U << height){
        data = alloc.allocate(length << 1); std::fill(data, data + length, monoid_type::identity_element);
        std::copy(first, last, data + length); std::fill(data.begin() + length + original_length, data + (length << 1), monoid_type::identity_element);
    }
    ~dualSegmentTree(void){ alloc.deallocate(data, (length << 1)); }

    // 区間更新 : O(logN)
    inline void update(unsigned int l, unsigned int r, const element_t& op){
        propagate(l += length); propagate((r += length) - 1);
        while(l < r){
            if(l & 1){ data[l] = monoid_type::operation(data[l], op); l++; }
            if(r & 1){ --r; data[r] = monoid_type::operation(data[r], op); }
            l >>= 1; r >>= 1;
        }
    }
  
    // 点更新 : O(logN)
    inline void update_at(unsigned int idx, const element_t& op){
        propagate(idx += length); data[idx] = monoid_type::operation(data[idx], op);
    }

    // 点取得 : 計算量 O(logN)
    inline element_t operator [](unsigned int idx) const {
        element_t res = data[idx += length]; while(idx >>= 1) res = monoid_type::operation(res, data[idx]);
        return res;
    }
    inline element_t at(unsigned int idx) const {
        assert(idx < original_length);
        element_t res = data[idx += length]; while(idx >>= 1) res = monoid_type::operation(res, data[idx]);
        return res;
    }
};

struct RangeUpdate{
    using value_type = unsigned long long int;
    static const value_type identity_element = 1ULL << 60;
    static constexpr value_type operation(const value_type& val, const value_type& op){
        return (val > op) ? op : val;
    }
};

struct edge{
    unsigned int l;
    unsigned int r;
    unsigned long long int dist;
    constexpr edge(void):l(), r(), dist(){}
    constexpr edge(const unsigned int l, const unsigned int r, const unsigned long long int dist): l(l), r(r), dist(dist){}
    inline bool operator < (const edge& other) const {
        if(l != other.l) return l < other.l;
        return r < other.r;
    }
};


int main(void){
    std::cin.tie(nullptr); std::cin.sync_with_stdio(false);
    std::cout.tie(nullptr); std::cout.sync_with_stdio(false);
    unsigned int n, m; std::cin >> n >> m;
    std::vector<edge> D(m);
    for(unsigned int i = 0; i < m; ++i) std::cin >> D[i].l >> D[i].r >> D[i].dist;
    std::sort(D.begin(), D.end());
    dualSegmentTree<RangeUpdate> RMQ(n);
    RMQ.update_at(0, 0);
    for(unsigned int i = 0; i < m; ++i){
        const unsigned long long int val = RMQ[D[i].l - 1];
        RMQ.update(D[i].l - 1, D[i].r, val + D[i].dist);
    }
    const unsigned long long int res = RMQ[n-1];
    if(res == RangeUpdate::identity_element) std::cout << "-1\n";
    else std::cout << res << '\n';
    return 0;
}

Measured memory_bytes: 3840