Problem:
Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#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;
}