#ifndef DEBUG
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>

//#define DEBUG
#ifdef DEBUG
#define Debug(...) cerr << "# Debug:" _ __PRETTY_FUNCTION__ _ '-' _ __LINE__ _ '(' _ #__VA_ARGS__ _ (__VA_ARGS__) _ ')' << endl;
#define FAST ((void)0);
#else
#define endl '\n'
#define Debug(x) ((void)0);
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#endif // DEBUG
#define _ << ' ' <<
#define Fr(i, s, e) for(auto i = s; i < e; ++i)
#define CASE int _T;cin >> _T;Fr(tt, 1, _T+1)
#define Eb emplace_back
#define Iter(v) v.begin(),v.end()
#define Lsort(v) sort(Iter(v))
#define Sz(v) ll(v.size())
#define Ft first
#define Sd second

using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

constexpr ld PI = acos(-1), eps = 1e-9;
constexpr ll MOD97 = 1e9+7;
constexpr ll MAX = 1e5+5;
constexpr ll INF = 1023456789;
constexpr ll IINF = 1234567890123456789LL;

inline ll getint() {
    ll x=0,s=1;char c=getchar();
    while((c<'0'||c>'9')&&c!='-')c=getchar();
    if(c=='-')c=getchar(),s=-1;
    while('0'<=c&&c<='9')x=x*10+c-'0',c=getchar();
    return x*s;
}

inline ll fpow(ll a, ll b, ll m = MOD97) {
    if (b == 0) return 1%m;
    if (b == 1) return a%m;
    ll r = fpow((a*a)%m, b/2, m);
    return (r*(b&1?a:1))%m;
}

struct SegTree {
    int val, l, r;
    SegTree *lc, *rc;
    SegTree(int _l, int _r): l(_l), r(_r) {
        val = INF;
        if (r-l == 1);
        else {
            int mid = (l+r)>>1;
            lc = new SegTree(l, mid);
            rc = new SegTree(mid, r);
        }
    }
    void update(int x, int nw) {
        if (x < l || r <= x) return;
        val = min(val, nw);
        if (r-l == 1) return;
        int mid = (l+r)>>1;
        if (x < mid) lc->update(x, nw);
        else rc->update(x, nw);
    }
    int query(int ql, int qr) {
        if (qr < l || r <= ql) return INF;
        if (ql == l && qr == r) return val;
        int mid = (l+r)>>1;
        if (qr <= mid) return lc->query(ql, qr);
        if (mid <= ql) return rc->query(ql, qr);
        return min(lc->query(ql, mid), rc->query(mid, qr));
    }
};
struct Oper {
    int l, r, c;
};

signed main() {
    FAST
    srand(time(NULL));

    int n, m;
    cin >> n >> m;
    vector<Oper> v(m);
    Fr(i, 0, m) cin >> v[i].l >> v[i].r >> v[i].c;
    Fr(i, 0, m) v[i].l--, v[i].r--;
    sort(Iter(v), [](const Oper &a, const Oper &b) {
        return a.l < b.l;
    });
    SegTree *root = new SegTree(0, n);
    root->update(0, 0);
    int x;
    Fr(i, 0, m) {
        x = root->query(v[i].l, v[i].r);
      	if (x != INF)
        	root->update(v[i].r, x+v[i].c);
    }
    x = root->query(n-1, n);
    if (x == INF) x = -1;
    cout << x << endl;

    return 0;
}
