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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define LL long long
#define INF 1<<31-1
#define LNF 9223372036854775807
#define PI 3.14159265358979
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define FOR(i,a,n) for(i=a;i<n;i++)
#define MOD 998244353
#define NUM 200010

void swap(int* a,int* b){int tmp=*a;*a=*b;*b=tmp;}
int dup(int a,int d){if(a%d==0)return a/d;else return a/d+1;}
int mod(int a,int m){if(a<0)return a%m+m;else return a%m;}
int GCD(int a,int b){int c=1,tmp=MAX(a,b);b=MIN(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
int LCM(int a,int b){return a*b/GCD(a,b);}
LL POW(LL a,LL n){LL ans=1;while(n>0){if(n&1)ans=ans*a%MOD;a=a*a%MOD;n>>=1;}return ans;}
int icmp(const void* a,const void* b){return *(int*)a-*(int*)b;}
int lcmp(const void* a,const void* b){if(*(LL*)a>*(LL*)b)return 1;if(*(LL*)a<*(LL*)b)return -1;return 0;}
int ccmp(const void* a,const void* b){return *(char*)a-*(char*)b;}
int scmp(const void* a,const void* b){return strcmp(*(char**)a,*(char**)b);}
LL fac[NUM],finv[NUM],inv[NUM];
void comi(){int i;fac[0]=fac[1]=1;finv[0]=finv[1]=1;inv[1]=1;FOR(i,2,NUM){fac[i]=fac[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
int com(int n,int k){if(n<k||n<0||k<0)return 0;return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;}


int pid=0,rpid[NUM];
LL cpid[NUM],que[NUM];
int judge(int a,int b){
  if(pid<a||pid<b||b==1)return 0;
  return cpid[que[a]]>cpid[que[b]]?1:0;
}
void push(LL a,int n){
  int i=++pid;
  rpid[i]=n,cpid[i]=a,que[i]=i;
  while(judge(i/2,i))que[i]=que[i/2],i/=2;
  que[i]=pid;
}
int pop(){
  int ans=rpid[que[1]],i=1,j;
  LL max=que[pid--];
  que[1]=max;
  while(judge(i,j=i*2+judge(i*2+1,i*2)))que[i]=que[j],que[j]=max,i=j;
  return ans;
}
LL Dijkstra(int v,int e,int* a,int* b,int* l){
  int i,ver=1,flag[NUM]={0},pre[e+1],last[e+1];
  LL cost[v+1];
  FOR(i,1,v+1)cost[i]=LNF,last[i]=-1;
  cost[1]=0;
  FOR(i,0,e)pre[i]=last[a[i]],last[a[i]]=i;
  while(flag[ver]==0){
    flag[ver]++;
    for(i=last[ver];i+1;i=pre[i])if(cost[b[i]]>cost[ver]+l[i]){
      cost[b[i]]=cost[ver]+l[i];
      push(cost[b[i]],b[i]);
    }
    while(flag[ver]&&pid)ver=pop();
  }
  return cost[v]<LNF?cost[v]:-1;
}

int main(){
  int N,M,i,ans=0;
  scanf("%d%d",&N,&M);
  int L[N+M-1],R[N+M-1],C[N+M-1];
  FOR(i,0,N-1)L[i]=i+2,R[i]=i+1,C[i]=0;
  FOR(i,N-1,N+M-1)scanf("%d%d%d",&L[i],&R[i],&C[i]);

  printf("%lld\n",Dijkstra(N,N+M-1,L,R,C));
	return 0;
}