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: """
  演算子は要素とセットでモノイドを形成するようなものでなければならない。
  すなわち、結合律が成り立ち単位元が存在する必要がある。
  
  equalityはsetを高速化するために使う。めんどい場合はNoneにしてもOK
  """
  
  
  class SegmentTree:
  
      @classmethod
      def all_identity(cls, operator, equality, identity, size):
          return cls(
              operator, equality, identity, [identity] * (2 << (size - 1).bit_length())
          )
  
      @classmethod
      def from_initial_data(cls, operator, equality, identity, data):
          size = 1 << (len(data) - 1).bit_length()
          temp = [identity] * (2 * size)
          temp[size : size + len(data)] = data
          data = temp
  
          for i in reversed(range(size)):
              data[i] = operator(data[2 * i], data[2 * i + 1])
          return cls(operator, equality, identity, data)
  
      # これ使わずファクトリーメソッド使いましょうね
      def __init__(self, operator, equality, identity, data):
          if equality is None:
              equality = lambda a, b: False
          self.op = operator
          self.eq = equality
          self.id = identity
          self.data = data
          self.size = len(data) // 2
  
      def _interval(self, a, b):
          a += self.size
          b += self.size
          ra = self.id
          rb = self.id
  
          data = self.data
          op = self.op
          while a < b:
              if a & 1:
                  ra = op(ra, data[a])
                  a += 1
              if b & 1:
                  b -= 1
                  rb = op(data[b], rb)
              a >>= 1
              b >>= 1
          return op(ra, rb)
  
      def __getitem__(self, i):
          if isinstance(i, slice):
              return self._interval(
                  0 if i.start is None else i.start,
                  self.size if i.stop is None else i.stop,
              )
          elif isinstance(i, int):
              return self.data[i + self.size]
  
      def __setitem__(self, i, v):
          i += self.size
          data = self.data
          op = self.op
          eq = self.eq
          while i and not eq(data[i], v):
              data[i] = v
              v = op(data[i ^ 1], v) if i & 1 else op(v, data[i ^ 1])
              i >>= 1
  
      def __iter__(self):
          return iter(self.data[self.size :])
  
  
  N, M = map(int, input().split())
  
  memo = sorted(tuple(map(int, input().split())) for _ in range(M))
  
  seg = SegmentTree.all_identity(min, None, float("inf"), N)
  
  seg[0] = 0
  
  for l, r, c in memo:
      seg[r - 1] = min(seg[r - 1], seg[l - 1 : r] + c)
  
  res = seg[N - 1]
  print(res if res != float("inf") else -1)
  
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"}]