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>
using namespace std;

// 区間に辺を張って最短距離問題を解く
// Verify: Codeforces Round #406 (Div. 1), problem: (B) Legacy <https://codeforces.com/contest/786/submission/93412867>
template <typename T = long long>
struct SegmentEdgeShortestPath
{
    int V;
    int h;
    int INVALID = -1;
    std::vector<std::vector<std::pair<int, T>>> to;
    SegmentEdgeShortestPath() = default;
    SegmentEdgeShortestPath(int V) : V(V), h(1) {
        while (h < V) h *= 2;
        to.resize(h * 3);
        for (int i = 1; i < h; i++) {
            to[i].emplace_back(i * 2, 0);
            to[i].emplace_back(i * 2 + 1, 0);
            int j = i * 2 + h * 2;
            if (j >= h * 3) j -= h * 2;
            to[j].emplace_back(i + h * 2, 0);
            to[j + 1].emplace_back(i + h * 2, 0);
        }
    }
    void _add_dfs(int now, int l, int r, int begin, int end, std::vector<int> &vec_add) {
        if (end <= l or r <= begin) return;
        if (l <= begin and end <= r) {
            vec_add.emplace_back(now);
            return;
        }
        _add_dfs(now * 2, l, r, begin, (begin + end) / 2, vec_add);
        _add_dfs(now * 2 + 1, l, r, (begin + end) / 2, end, vec_add);
    }

    std::vector<int> from_vs, to_vs;
    void add_edge(int from_l, int from_r, int to_l, int to_r, T len) {
        from_vs.clear(), to_vs.clear();
        _add_dfs(1, from_l, from_r, 0, h, from_vs);
        _add_dfs(1, to_l, to_r, 0, h, to_vs);
        for (auto &x : from_vs) if (x < h) x += h * 2;
        for (const auto s : from_vs) {
            for (const auto t : to_vs) {
                to[s].emplace_back(t, len);
            }
        }
    }
    std::vector<T> dist;
    std::vector<T> _d_tmp;
    void solve01dfs(int s) {
        assert(s >= 0 and s < V);
        s += h;
        _d_tmp.assign(h * 3, std::numeric_limits<T>::max() / 2);
        _d_tmp[s] = 0;
        std::deque<int> q;
        q.emplace_back(s);
        while (q.size()) {
            int now = q.front();
            q.pop_front();
            for (const auto &p : to[now]) {
                const int nxt = p.first, d = p.second;
                if (_d_tmp[nxt] > _d_tmp[now] + d) {
                    _d_tmp[nxt] = _d_tmp[now] + d;
                    if (d) q.emplace_back(nxt);
                    else q.emplace_front(nxt);
                }
            }
        }
        dist.resize(V);
        std::copy(_d_tmp.begin() + h, _d_tmp.begin() + h + V, dist.begin());
    }

    void solve_dijkstra(int s) {
        assert(s >= 0 and s < V);
        s += h;
        _d_tmp.assign(h * 3, std::numeric_limits<T>::max() / 2);
        _d_tmp[s] = 0;
        using P = std::pair<T, int>;
        std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
        pq.emplace(0, s);
        while (pq.size()) {
            auto a = pq.top();
            pq.pop();
            int now = a.second;
            if (_d_tmp[now] < a.first) continue;
            for (const auto p : to[now])
            {
                int nxt = p.first;
                T d = p.second;
                if (_d_tmp[nxt] > _d_tmp[now] + d) {
                    _d_tmp[nxt] = _d_tmp[now] + d;
                    pq.emplace(_d_tmp[nxt], nxt);
                }
            }
        }
        dist.resize(V);
        std::copy(_d_tmp.begin() + h, _d_tmp.begin() + h + V, dist.begin());
    }
};

int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    SegmentEdgeShortestPath<long long> graph(N + M);
    while (M--)
    {
        int l, r, c;
        cin >> l >> r >> c;
        graph.add_edge(l - 1, r, N + M, N + M + 1, c);
        graph.add_edge(N + M, N + M + 1, l - 1, r, 0);
    }

    graph.solve_dijkstra(0);
    cout << (graph.dist[N - 1] < 1e18 ? graph.dist[N - 1] : -1) << '\n';
}