/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iterator>
#include <random>
#include <cassert>
#include <iomanip>
#define FOR(i, a, b) for(int (i)=(a);(i)<(b);++(i))
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cout<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<char> vc;
typedef vector<vector<int>> vii;
typedef vector<vector<lint>> vll;
typedef vector<vector<char>> vcc;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}
const double EPS = 1e-10;
const double PI = acos(-1);
const lint INF = (lint)1e18;

template <class T>
class SegmentTree{
private:
    // 点更新セグメント木
    int n;
    vector<T> dat;
    lint def = INF;

    // 区間クエリ
    T _query(int a, int b, int k, int l, int r){
        // [a, b), [l, r)
        if(r<=a or b<=l) return def;
        else if(a<=l and r<=b) return dat[k];
        else{
            T c1 = _query(a, b, 2*k+1, l, (l+r)/2);
            T c2 = _query(a, b, 2*k+2, (l+r)/2, r);
            return min(c1, c2);
        }
    }

    // 更新クエリ
    void _change(int i, T x){
        i += n - 1;
        dat[i] = min(dat[i], x);
        while(i){
            i = (i-1) / 2;
            dat[i] = min(dat[2*i+1], dat[2*i+2]);
        }
    }

public:
    // [a, b)
    T query(int a, int b){
        return _query(a, b, 0, 0, n);
    }

    void change(int i, T x){
        _change(i, x);
    }

    T operator[](int i){
        return dat[i+n-1];
    }

    // デフォルトコンストラクタ
    SegmentTree() : n(0) {}

    // コンストラクタ
    explicit SegmentTree(size_t _n){
        n = 1;
        while(n < _n){
            n *= 2;
        }
        dat = vector<T>(2*n-1, def);
    }
};

struct seg{int left, right; lint cost;};

bool cmp(seg s1, seg s2){
    if(s1.right!=s2.right) return s1.right < s2.right;
    return s1.left < s2.left;
}

class DShortestPathOnALine {
public:
    void solve(std::istream& in, std::ostream& out){
        ios_base::sync_with_stdio(false);
        in.tie(nullptr), out.tie(nullptr);
        int N, M;
        in>>N>>M;
        SegmentTree<lint> dat(N);
        vector<seg> A(M);
        REP(i, M) {
            in>>A[i].left>>A[i].right>>A[i].cost;
            A[i].left--, A[i].right--;
        }
        sort(ALL(A), cmp);
        dat.change(0, 0);
        for(auto s : A){
            lint dist = dat.query(s.left, s.right+1);
            dat.change(s.right, dist+s.cost);
        }
        lint ans = dat[N-1];
        if(ans==INF) out<<"-1\n";
        else out<<ans<<'\n';
    }
};


int main() {
	DShortestPathOnALine solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 3840