#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 1e5+7, INF = 5e18;
bool live[N << 2];
int prop[N << 2], mn[N << 2];
void build(int v, int tl, int tr) {
    mn[v] = INF;
    prop[v] = INF;
    live[v] = 1;
    if (tl == tr)
        return;
    int tm = (tl + tr) >> 1;
    build(v * 2 + 1, tl, tm);
    build(v * 2 + 2, tm + 1, tr);                        
}   
void gap(int v, int x) {
    if (!live[v])
        return;

    prop[v] = min(prop[v], x);
    mn[v] = min(mn[v], x);
}   
void push(int v) {
    if (prop[v] != INF) {
        gap(v * 2 + 1, prop[v]);
        gap(v * 2 + 2, prop[v]);
        prop[v] = INF;
    }   
}   
void relax(int v) {
    live[v] = live[v * 2 + 1] || live[v * 2 + 2];
    mn[v] = min(mn[v * 2 + 1], mn[v * 2 + 2]);
}   
void upd(int v, int tl, int tr, int l, int r, int x) {
    if (tr < l || r < tl)
        return;
    if (l <= tl && tr <= r) {
        gap(v, x);
        return;
    }   
    int tm = (tl + tr) >> 1;
    push(v);
    upd(v * 2 + 1, tl, tm, l, r, x);
    upd(v * 2 + 2, tm + 1, tr, l, r, x);
    relax(v);
}   
ii get(int v, int tl, int tr) {
    //cout << "get " << tl << ' ' << tr << ' ' << mn[v] << /*' ' << live[v] << */endl;

    if (mn[v] == INF || !live[v]) {
        return mp(-1, -1);
    }
    if (tl == tr) {
        live[v] = 0;
        int ret = mn[v];
        mn[v] = INF;
        //cout << "ban " << tl << endl;
        return mp(tl, ret);
    }   
    int tm = (tl + tr) >> 1;
    push(v);
    if (mn[v * 2 + 1] == mn[v]) {
        auto t = get(v * 2 + 1, tl, tm);
        relax(v);

        //cout << "relax " << tl << ' ' << tr << ' ' << mn[v] << endl;

        if (t.f != -1)
            return t;
    }   
    if (mn[v * 2 + 2] == mn[v]) {
        auto t = get(v * 2 + 2, tm + 1, tr);
        relax(v);
        if (t.f != -1)
            return t;
    }       
    return mp(-1, -1);
}   

int n, m;
int mx[N << 2];

struct Seg {
    int l, r, c;
};  
vector <Seg> seg;

void build1(int v, int tl, int tr) {
    if (tl == tr) {
        mx[v] = seg[tl].r;
        return;
    }   
    int tm = (tl + tr) >> 1;
    build1(v * 2 + 1, tl, tm);
    build1(v * 2 + 2, tm + 1, tr);
    mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}
void go(int v, int tl, int tr, int l, int r, int u, int d) {
    if (tr < l || r < tl || mx[v] < u) {
        return;        
    }   
    if (tl == tr) {
        upd(0, 1, n, seg[tl].l, seg[tl].r, d+seg[tl].c);
        mx[v] = -1;
        return;
    }   
    int tm = (tl + tr) >> 1;
    go(v * 2 + 1, tl, tm, l, r, u, d);
    go(v * 2 + 2, tm + 1, tr, l, r, u, d);
    mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}   

bool comp(Seg a, Seg b) { return a.l < b.l; }

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    cin >> n >> m;
    vector <int> l(m), r(m), c(m);
    for (int i = 0; i < m; ++i) {
        cin >> l[i] >> r[i] >> c[i];
        seg.app({l[i], r[i], c[i]});
    }   
    sort(all(seg), comp);

    build(0, 1, n);
    build1(0, 0, (int)seg.size()-1);
    upd(0, 1, n, 1, 1, 0);

    while (1) {
        auto t = get(0, 1, n);
        if (t.f == -1)
            break;
        if (t.f == n) {
            cout << t.s << endl;
            exit(0);
        }   
        int u = t.f, d = t.s;
        {

        int l = -1;
        int r = seg.size();
        while (l < r - 1) {
            int m = (l + r) >> 1;
            if (seg[m].l <= u)
                l = m;
            else
                r = m;
        }   
        go(0, 0, (int)seg.size() - 1, 0, l, u, d);
        }
    }   

    cout << -1 << endl;
}

Measured memory_bytes: 14412