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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(long long i=0;i<x;i++)
#define repn(i,x) for(long long i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
vector<pair<string,P> >vec;
// vector<vector<int>> data(3, vector<int>(4));


const int MAX_N = 1 << 20;
// セグメント木を持つグローバル配列 
int n, dat[2 * MAX_N - 1];
// 初期化
void init(int n_) {
	// 簡単のため、要素数を2のべき乗に 
	n = 1;
	while (n < n_) n *= 2;
	// すべての値をINT_MAX/2に
	for (int i = 0; i < 2 * n - 1; i++) dat[i] = INT_MAX/2; 
}
// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
	// 葉の節点
	k += n - 1; 
	dat[k] = a; 
	// 登りながら更新
	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}

int query(int a, int b, int k, int l, int r) {
	// [a, b)と[l, r)が交差しなければ、INT_MAX/2 
	if (r <= a || b <= l) return INT_MAX/2;
	// [a, b)が[l, r)を完全に含んでいれば、この節点の値 
	if (a <= l && r <= b) return dat[k]; 
	else {
		// そうでなければ、2つの子の最小値
		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); 
		int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	} 
}



vector<int> dp(100001, INT_MAX/2);

int main(){	
	int M;
	int N;
	scanf("%d",&N);
	init(N);
	scanf("%d",&M);
	vector<int> C(M-1+1);
	vector<int> L(M-1+1);
	vector<int> R(M-1+1);
	vector<vector<int>> lv(M, vector<int>(3));
	for(int i = 0 ; i <= M-1 ; i++){
		scanf("%d",&L[i]);
		scanf("%d",&R[i]);
		scanf("%d",&C[i]);
		L[i]--; R[i]--;
		lv[i][0] = L[i];
		lv[i][1] = R[i];
		lv[i][2] = C[i];
	}
	dp[0] = 0;
	update(0, 0);
	sort(lv.begin(),lv.end());
	// rep(i, M) {
	// 	cout << lv[i][0] << " " << lv[i][1] << " " << lv[i][2] << endl;
	// }
	rep(i, M) {
		int l = lv[i][0], r = lv[i][1], c = lv[i][2];
		// cout << lv[i][0] << " " << lv[i][1] << " " << lv[i][2] << endl;
		
		dp[r] = min(dp[r], query(l, r+1, 0, 0, n)+c);
		update(r, dp[r]);

		// rep(j, N+1) if (dp[j] >= INT_MAX/4) cout << "i"; else cout << dp[j];
		// cout << endl;
	}

	if (dp[N-1] >= INT_MAX/2 ) printf("-1\n"); else printf("%d\n",dp[N-1]);
	return 0;
}