output_description: 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.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      L_1 R_1 C_1
      :
      L_M R_M C_M
attempt: # -*- coding: utf-8 -*-
  """
  Created on Sun Aug  4 22:01:34 2019
  
  @author: miz18
  """
  
  
  class SegmentTree:
      def __init__(self, L, op, ide):
          self.op = op
          self.ide = ide
          self.sz = len(L)
          self.n = 1
          self.s = 1
          for i in range(1000):
              self.n *= 2
              self.s += 1
              if self.n >= self.sz:
                  break
          self.node = [self.ide] * (2 * self.n - 1)
  
          for i in range(self.sz):
              self.node[i + self.n - 1] = L[i]
  
          for i in range(self.n - 2, -1, -1):
              self.node[i] = self.op(self.node[i * 2 + 1], self.node[i * 2 + 2])
  
      def add(self, a, x):
          k = a + self.n - 1
          self.node[k] += x
          for i in range(1000):
              k = (k - 1) // 2
              self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
              if k <= 0:
                  break
  
      def substitute(self, a, x):
          k = a + self.n - 1
          self.node[k] = min(x, self.node[k])
          for i in range(1000):
              k = (k - 1) // 2
              self.node[k] = self.op(self.node[2 * k + 1], self.node[2 * k + 2])
              if k <= 0:
                  break
  
      def get_one(self, a):
          k = a + self.n - 1
          return self.node[k]
  
      def get(self, l, r):
          res = self.ide
          n = self.n
          if self.sz <= r or 0 > l:
              print("ERROR: the indice are wrong.")
              return False
  
          for i in range(self.s):
              count = 2**i - 1
              a = (r + 1) // n
              b = (l - 1) // n
              if a - b == 3:
                  res = self.op(self.node[count + b + 1], res)
                  res = self.op(self.node[count + b + 2], res)
                  right = a * n
                  left = (b + 1) * n - 1
                  break
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  right = a * n
                  left = (b + 1) * n - 1
                  break
              n = n // 2
  
          # left
          n1 = n // 2
          for j in range(i + 1, self.s):
              count = 2**j - 1
              a = (left + 1) // n1
              b = (l - 1) // n1
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  left = (b + 1) * n1 - 1
              n1 = n1 // 2
  
          # right
          n1 = n // 2
          for j in range(i + 1, self.s):
              count = 2**j - 1
              a = (r + 1) // n1
              b = (right - 1) // n1
              if a - b == 2:
                  res = self.op(self.node[count + b + 1], res)
                  right = a * n1
              n1 = n1 // 2
          return res
  
  
  N, M = list(map(int, input().split()))
  
  road = []
  for i in range(M):
      road.append(list(map(int, input().split())))
  
  road.sort(key=lambda x: x[2])
  road.sort(key=lambda x: x[0])
  
  R = [10**27] * N
  R[0] = 0
  ST = SegmentTree(R, min, 10**27)
  
  for i in range(M):
      now_min = ST.get(road[i][0] - 1, road[i][1] - 1)
      ST.substitute(road[i][1] - 1, now_min + road[i][2])
  ans = ST.get_one(N - 1)
  
  if ans >= 10**27:
      print(-1)
  else:
      print(ans)
  
problem_description: 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.
samples: [{"input": "4 3\n    1 3 2\n    2 4 3\n    1 4 6", "output": "5\n    \n\nWe have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of\nlength 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between\nVertex 1 and Vertex 4.\n\n* * *"}, {"input": "4 2\n    1 2 1\n    3 4 2", "output": "-1\n    \n\n* * *"}, {"input": "10 7\n    1 5 18\n    3 4 8\n    1 3 5\n    4 7 10\n    5 9 8\n    6 10 5\n    8 10 3", "output": "28"}]