//
// Created by kuribohkute on 20/04/2019.
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
#define mp make_pair
#define X first
#define Y second
#define CL(x) (x << 1)
#define CR(x) (CL(x) + 1)
const int N = 100005;
int n, m;
long long a[N];
const long long oo = 1ll * N * 1003 * 1003 * 1003;

struct Segtree {
    long long it[4 * N], lz[4 * N];
    int L[4 * N], H[4 * N];
    void build(int x, int l, int h) {
        L[x] = l;
        H[x] = h;
        lz[x] = oo;
        if (l == h) {
            it[x] = a[l];
            return;
        }
        int mid = (l + h) / 2;
        build (CL(x), l, mid);
        build (CR(x), mid + 1, h);
        it[x] = min(it[CL(x)], it[CR(x)]);
    }
    void down(int x) {
        it[CL(x)] = min(it[CL(x)], lz[x]);
        it[CR(x)] = min(it[CR(x)], lz[x]);
        lz[CL(x)] = min(lz[CL(x)], lz[x]);
        lz[CR(x)] = min(lz[CR(x)], lz[x]);
        lz[x] = oo;
    }
    void update(int x, int u, int v, long long val) {
        if (L[x] > v || H[x] < u) {
            return;
        }
        if (u <= L[x] && H[x] <= v) {
            it[x] = min(it[x], val);
            lz[x] = val;
            return;
        }
        if (lz[x] != oo) {
            down(x);
        }
        update(CL(x), u, v, val);
        update(CR(x), u, v, val);
        it[x] = min(it[CL(x)], it[CR(x)]);
    }
    long long get(int x, int u, int v) {
        if (L[x] > v || H[x] < u) {
            return oo;
        }
        if (L[x] == H[x]) {
            return it[x];
        }
        if (lz[x] != oo) {
            down(x);
        }
        return min(get(CL(x), u, v), get(CR(x), u, v));
    }
} IT;

struct Segs {
    int l, r, c;
    Segs(int _l, int _r, int _c) {
        l = _l;
        r = _r;
        c = _c;
    }
    bool operator < (Segs x) {
        return r < x.r;
    }
};
vector <Segs> segs;

int main () {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        a[i] = oo;
    }
    a[1] = 0;
    IT.build(1, 1, n);
    for (int i = 0; i < m; i++) {
        int l, r, c;
        cin >> l >> r >> c;
        segs.emplace_back(Segs(l, r, c));
    }
    sort(segs.begin(), segs.end());
    for (auto seg: segs) {
        long long mn = IT.get(1, seg.l, seg.r);
//        cout << seg.l << " "<< seg.r << " " << seg.c << "\n";
        IT.update(1, seg.l, seg.r, mn + seg.c);
    }
//    cout << oo << "\n";for (int i = 1; i <= n; i++) {cout << IT.get(1, i, i) << " ";}cout << "\n";
    long long res = IT.get(1, n, n);
    if (res == oo) {
        cout << -1;
    } else {
        cout << res;
    }
    return 0;
}
