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: import sys
  
  input = sys.stdin.readline
  
  
  def I():
      return int(input())
  
  
  def MI():
      return map(int, input().split())
  
  
  def LI():
      return list(map(int, input().split()))
  
  
  def main():
      N, M = MI()
      inf = 10**16
  
      class SegTree:
          def __init__(self, N, ide, segfunc=min):
              self.ide_ele = ide
              """
              ex)
               最小値のセグ木 → +inf
                和のセグ木 → 0
                積のセグ木 → 1
                gcdのセグ木 → 0
              """
  
              self.segfunc = segfunc
  
              # num:N以上の最小の2のべき乗
              self.num = 2 ** (N - 1).bit_length()
              self.seg = [self.ide_ele] * 2 * self.num
  
          # リストで初期化する
          def setL(self, init_val):
              # init_valは操作する数列
              for i in range(N):
                  self.seg[i + self.num - 1] = init_val[i]
              # built
              for i in range(self.num - 2, -1, -1):
                  self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])
  
          # k番目の値をxに更新
          def update(self, k, x):
              k += self.num - 1
              self.seg[k] = x
              while k:
                  k = (k - 1) // 2
                  self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])
  
          # [p,q)の区間に対するクエリへの応答
          def query(self, p, q):
              if q <= p:
                  return self.ide_ele
              p += self.num - 1
              q += self.num - 2
              res = self.ide_ele
              while q - p > 1:
                  if p & 1 == 0:
                      res = self.segfunc(res, self.seg[p])
                  if q & 1 == 1:
                      res = self.segfunc(res, self.seg[q])
                      q -= 1
                  p = p // 2
                  q = (q - 1) // 2
              if p == q:
                  res = self.segfunc(res, self.seg[p])
              else:
                  res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q])
              return res
  
      seg = SegTree(N + 1, inf, min)
  
      L = [0] * M
      R = [0] * M
      C = [0] * M
      for i in range(M):
          l, r, c = MI()
          l -= 1
          r -= 1
          L[i] = l
          R[i] = r
          C[i] = c
  
      R, C, L = zip(*sorted(zip(R, C, L)))
  
      # rが小さいものから見て(rが同じならcが小さい)確定させていく
      now = 0  # 確定させたとこと
      seg.update(0, 0)
  
      for i in range(M):
          r = R[i]
          l = L[i]
          c = C[i]
          m = seg.query(l, now + 1)  # 移動もとの範囲での最小値
          # seg.update(r,m+c)
          now = r
  
          # このままだと入力例3みたいに，コストは低いけどl,rがちかいみたいなのを先に処理してしまう
          # とりあえず右端だけ更新しとけばokか?
  
          if m + c < seg.query(now, now + 1):
              seg.update(now, m + c)
  
          # print((l+1,r+1,c),now+1,m,m+c)
          """
          for ii in range(N):
              print(seg.query(ii,ii+1))"""
  
      ans = seg.query(N - 1, N)
  
      if ans >= inf:
          print(-1)
      else:
          print(ans)
  
  
  main()
  
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"}]