atcoder#NIKKEI20192QUALD. Shortest Path on a Line

Shortest Path on a Line

Score : 600600 points

Problem Statement

We have NN points numbered 11 to NN 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 MM operations to add edges in this graph. The ii-th operation is as follows:

  • The operation uses integers LiL_i and RiR_i between 11 and NN (inclusive), and a positive integer CiC_i. For every pair of integers (s,t)(s, t) such that Lis<tRiL_i \leq s < t \leq R_i, add an edge of length CiC_i between Vertex ss and Vertex tt.

The integers L1,...,LML_1, ..., L_M, R1,...,RMR_1, ..., R_M, C1,...,CMC_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 11 to Vertex NN in the final graph.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1M1051 \leq M \leq 10^5
  • 1Li<RiN1 \leq L_i < R_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

NN MM

L1L_1 R1R_1 C1C_1

::

LML_M RMR_M CMC_M

Output

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

4 3
1 3 2
2 4 3
1 4 6
5

We have an edge of length 22 between Vertex 11 and Vertex 22, and an edge of length 33 between Vertex 22 and Vertex 44, so there is a path of length 55 between Vertex 11 and Vertex 44.

4 2
1 2 1
3 4 2
-1
10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3
28