#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<P,ll> PP;
typedef vector<vector<ll> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

struct SegmentTreeBeats {
    static const long long INF = 1LL << 60;
    struct Node {
        Node *left, *right;
        long long max_v, secmax_v, max_c;
        long long min_v, secmin_v, min_c;
        long long sum;
        long long len, l_add, l_val;

        Node() : left(0), right(0), l_add(0), l_val(INF) {}

        void init(long long x) {
            max_v = min_v = sum = x;
            secmax_v = -INF; secmin_v = INF;
            max_c = min_c = 1;
        }

        void init_empty() {
            max_v = secmax_v = -INF;
            min_v = secmin_v = INF;
            max_c = min_c = 0;
        }

        void update_max(long long x) {
            sum += (x - max_v) * max_c;
            if (max_v ==  min_v) max_v =  min_v = x;
            else if (max_v == secmin_v) max_v = secmin_v = x;
            else                      max_v = x;
            if (l_val != INF and x < l_val) l_val = x;
        }

        void update_min(long long x) {
            sum += (x - min_v) * min_c;
            if (max_v ==  min_v) max_v =  min_v = x;
            else if (max_v == secmin_v) min_v = secmax_v = x;
            else                      min_v = x;
            if (l_val != INF and l_val < x) l_val = x;
        }

        void addall(long long x) {
            max_v += x;
            if (secmax_v != -INF) secmax_v += x;
            min_v += x;
            if (secmin_v != INF) secmin_v += x;
            sum += len * x;
            if (l_val != INF) l_val += x;
            else             l_add += x;
        }

        void updateall(long long x) {
            max_v = min_v = x;
            secmax_v = -INF; secmin_v = INF;
            max_c = min_c = len;
            sum = len * x;
            l_val = x; l_add = 0;
        }

        void push() {
            if (l_val != INF) {
                 left -> updateall(l_val);
                right -> updateall(l_val);
                l_val = INF;
                return;
            }
            if (l_add != 0) {
                 left -> addall(l_add);
                right -> addall(l_add);
                l_add = 0;
            }
            if (max_v < left -> max_v)   left -> update_max(max_v);
            if (left -> min_v < min_v)   left -> update_min(min_v);
            if (max_v < right -> max_v) right -> update_max(max_v);
            if (right -> min_v < min_v) right -> update_min(min_v);
        }

        void update() {
            sum = left -> sum + right -> sum;
            if (left -> max_v < right -> max_v) {
                max_v = right -> max_v;
                max_c = right -> max_c;
                secmax_v = max(left -> max_v, right -> secmax_v);
            } else if (left -> max_v > right -> max_v) {
                max_v = left -> max_v;
                max_c = left -> max_c;
                secmax_v = max(left -> secmax_v, right -> max_v);
            } else {
                max_v = left -> max_v;
                max_c = left -> max_c + right -> max_c;
                secmax_v = max(left -> secmax_v, right -> secmax_v);
            }
            if (left -> min_v < right -> min_v) {
                min_v = left -> min_v;
                min_c = left -> min_c;
                secmin_v = min(left -> secmin_v, right -> min_v);
            } else if (left -> min_v > right -> min_v) {
                min_v = right -> min_v;
                min_c = right -> min_c;
                secmin_v = min(left -> min_v, right -> secmin_v);
            } else {
                min_v = left -> min_v;
                min_c = left -> min_c + right -> min_c;
                secmin_v = min(left -> secmin_v, right -> secmin_v);
            }
        }
    };

    int n, n0;
    Node *root;

    void sub_update_min(long long x, int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a || nd -> max_v <= x) return;
        if (a <= l && r <= b && nd -> secmax_v < x) {
            nd -> update_max(x);
            return;
        }
        nd -> push();
        sub_update_min(x, a, b, nd ->  left, l, (l + r) >> 1);
        sub_update_min(x, a, b, nd -> right, (l + r) >> 1, r);
        nd -> update();
    }

    void sub_update_max(long long x, int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a || x <= nd -> min_v) return;
        if (a <= l && r <= b && x < nd -> secmin_v) {
            nd -> update_min(x);
            return;
        }
        nd -> push();
        sub_update_max(x, a, b, nd ->  left, l, (l + r) >> 1);
        sub_update_max(x, a, b, nd -> right, (l + r) >> 1, r);
        nd -> update();
    }

    void sub_add_val(long long x, int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            nd -> addall(x);
            return;
        }
        nd -> push();
        sub_add_val(x, a, b, nd ->  left, l, (l + r) >> 1);
        sub_add_val(x, a, b, nd -> right, (l + r) >> 1, r);
        nd -> update();
    }

    void sub_update_val(long long x, int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            nd -> updateall(x);
            return;
        }
        nd -> push();
        sub_update_val(x, a, b, nd ->  left, l, (l + r) >> 1);
        sub_update_val(x, a, b, nd -> right, (l + r) >> 1, r);
        nd -> update();
    }

    long long sub_query_max(int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a) return -INF;
        if (a <= l && r <= b) return nd -> max_v;
        nd -> push();
        long long lv = sub_query_max(a, b, nd ->  left, l, (l + r) >> 1);
        long long rv = sub_query_max(a, b, nd -> right, (l + r) >> 1, r);
        return max(lv, rv);
    }

    long long sub_query_min(int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a) return INF;
        if (a <= l && r <= b) return nd -> min_v;
        nd -> push();
        long long lv = sub_query_min(a, b, nd ->  left, l, (l + r) >> 1);
        long long rv = sub_query_min(a, b, nd -> right, (l + r) >> 1, r);
        return min(lv, rv);
    }

    long long sub_query_sum(int a, int b, Node *nd, int l, int r) {
        if (b <= l || r <= a) return 0;
        if (a <= l && r <= b) return nd -> sum;
        nd -> push();
        long long lv = sub_query_sum(a, b, nd ->  left, l, (l + r) >> 1);
        long long rv = sub_query_sum(a, b, nd -> right, (l + r) >> 1, r);
        return lv + rv;
    }

    SegmentTreeBeats(int n, long long val = 0) : n(n) {
        n0 = 1; while (n0 < n) n0 <<= 1;
        Node *nds = new Node[2 * n0];
        root = nds;
        nds[0].len = n0;
        for (int i = 0; i < n0 - 1; ++i) {
            nds[i].left = &nds[2 * i + 1];
            nds[i].right = &nds[2 * i + 2];
            nds[2 * i + 1].len = nds[2 * i + 2].len = nds[i].len >> 1;
        }
        for (int i = 0; i <  n; ++i) nds[n0 - 1 + i].init(val);
        for (int i = n; i < n0; ++i) nds[n0 - 1 + i].init_empty();
        for (int i = n0 - 2; i >= 0; i--) nds[i].update();
    }

    SegmentTreeBeats(vector<long long> a) : n(a.size()) {
        n0 = 1; while (n0 < n) n0 <<= 1;
        Node *nds = new Node[2 * n0];
        root = nds;
        nds[0].len = n0;
        for (int i = 0; i < n0 - 1; ++i) {
            nds[i].left = &nds[2 * i + 1];
            nds[i].right = &nds[2 * i + 2];
            nds[2 * i + 1].len = nds[2 * i + 2].len = nds[i].len >> 1;
        }
        for (int i = 0; i <  n; ++i) nds[n0 - 1 + i].init(a[i]);
        for (int i = n; i < n0; ++i) nds[n0 - 1 + i].init_empty();
        for (int i = n0 - 2; i >= 0; i--) nds[i].update();
    }

    void update_min(int a, int b, long long x) { sub_update_min(x, a, b, root, 0, n0); }
    void update_max(int a, int b, long long x) { sub_update_max(x, a, b, root, 0, n0); }
    void add_val(int a, int b, long long x)    { sub_add_val(x, a, b, root, 0, n0); }
    void update_val(int a, int b, long long x) { sub_update_val(x, a, b, root, 0, n0); }
    long long query_max(int a, int b) { return sub_query_max(a, b, root, 0, n0); }
    long long query_min(int a, int b) { return sub_query_min(a, b, root, 0, n0); }
    long long query_sum(int a, int b) { return sub_query_sum(a, b, root, 0, n0); }
};

struct query{
    ll l,r,c;

    bool operator<( const query& que) const {
        return l < que.l;
    }
};

//入力
ll n,m;

int main(){
    cin >> n >> m;
    SegmentTreeBeats seg(n);
    vector<query>que(m);
    rep(i,m){
        ll l,r,c;
        cin >> l >> r >> c;
        l--;r--;
        query now = {l,r,c};
        que[i]=now;
    }
    sort(que.begin(),que.end());
    seg.update_val(0,n,INF);
    seg.update_val(0,1,0);
    rep(i,m){
        ll now = seg.query_sum(que[i].l,que[i].l+1);
        seg.update_min(que[i].l,que[i].r+1,now + que[i].c);
    }
    ll ans = seg.query_sum(n-1,n);
    if(ans==INF)cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}

Measured memory_bytes: 30180