#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <deque>
#include <functional>
#include <initializer_list>
#include <math.h>
#include <map>
#include <random>
#include <set>
#include <stack>
#include <tuple>
#include <queue>
#include <vector>
#include <type_traits>

#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); --(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) rFOR(i, 0, (n-1))
#define SORT(A) std::sort((A).begin(), (A).end())
#define ALL(A) (A).begin(), (A).end()
// 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A)
#define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end())

using lli = long long int;
using pii = std::pair<int, int>;

// グリッド上の縦横移動
constexpr std::array<std::pair<int, int>, 4> dxdy = {
    std::pair<int, int>( 1,  0),
    std::pair<int, int>(-1,  0),
    std::pair<int, int>( 0,  1),
    std::pair<int, int>( 0, -1)
};

void VintOut(std::vector<int>& A){
    const int n = A.size();
    if(n == 0){putchar('\n'); return;}
    printf("%d", A[0]);
    for(int i = 1; i < n; ++i) printf(" %d", A[i]);
    putchar('\n');
}
void VintOut(std::vector<long long int>& A){
    const int n = A.size();
    if(n == 0){ putchar('\n'); return;}
    printf("%lld", A[0]);
    for(int i = 1; i < n; ++i) printf(" %lld", A[i]);
    putchar('\n');
}

template <typename T>
inline bool chmin(T& a, T b){
    if(b < a){ a = b; return true;}
    return false;
}

template <typename T>
inline bool chmax(T& a, T b){
    if(a < b){ a = b; return true;}
    return false;
}

inline bool isIn(int x, int y, int H, int W){
    return 0 <= x and x < H and 0 <= y and y < W;
}
inline bool bitUP(int state, int k){ return (state >> k) & 1; }
inline bool bitUP(long long int state, int k){ return (state >> k) & 1;}

// z-algorithm
template <class T> std::vector<int> z_algorithm(const T &str) {
    const int n = str.size();
    std::vector<int> resOfCP(n); resOfCP[0] = n;
    int i = 1, j = 0;
    while (i < n) {
        while (i + j < n and str[j] == str[i + j]) ++j;
        resOfCP[i] = j;
        if (j == 0) { ++i; continue;}
        int k = 1;
        while (i + k < n and k + resOfCP[k] < j) resOfCP[i + k] = resOfCP[k], ++k;
        i += k; j -= k;
    }
    return resOfCP;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// < 双対セグメント木クラス >
// @ 概要 | 区間更新・点取得が高速に行える 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;                            // 配列の要素の数の指数部 : 
    const unsigned int length;                            // 配列の要素の数 : 2^k (2のべき乗)k
    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){
            jp = idx >> i; jc = jp << 1;
            if(data[jp] == monoid_type::identity_element) continue;
            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 + 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 = long long int;
    static const value_type identity_element = 1LL << 60;
    static value_type operation(const value_type& val, const value_type& op){
        return (val > op) ? op : val;
    }
};

std::vector<std::pair<pii, int>> D;

int main(void){
    int n, m; scanf("%d%d", &n, &m);
    D.resize(m); REP(i, m){
        int l, r, c; scanf("%d%d%d", &l, &r, &c);
        D[i] = {{l, r}, c};
    }
    std::sort(D.begin(), D.end());
    dualSegmentTree<RangeUpdate> RMQ(n);
    RMQ.update_at(0, 0);
    REP(i, m){
        const std::pair<int, int>& pr = D[i].first;
        const long long int val = RMQ[pr.first-1];
        RMQ.update(pr.first, pr.second, val + D[i].second);
    }
    const long long int res = RMQ[n-1];
    if(res == RangeUpdate::identity_element) puts("-1");
    else printf("%lld\n", res);
    return 0;
}

Measured memory_bytes: 3456