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
  
      ##########################################
      import heapq
  
      class Dijkstra:
          """
          ・有向 / 無向は問わない(無向の場合は，逆向きの辺もたす)
          ・負のコストがない場合のみ
          ・計算量はO(E log|V|)
          ・heapを使うことで頂点を走査する必要がなくなる（代わりに，距離更新したものは確定でなくともqueに入れておく）
          """
  
          # 最短のpathをたす
  
          class Edge:
              # 重み付き有向辺
  
              def __init__(self, _to, _cost):
                  self.to = _to
                  self.cost = _cost
  
          def __init__(self, V):
              # 引数Vは頂点数
              self.G = [[] for _ in range(V)]  # 隣接リストG[u][i]が頂点uのi番目の辺
              self._E = 0  # 辺の数
              self._V = V  # 頂点数
  
          # proparty - 辺の数
          def E(self):
              return self._E
  
          # proparty - 頂点数
          def V(self):
              return self._V
  
          def add(self, _from, _to, _cost):
              # 2頂点と辺のコストを追加
              self.G[_from].append(self.Edge(_to, _cost))
              self._E += 1
  
          def add2(self, _from, _to, _cost):
              # 2頂点と辺のコスト（無向）を追加
              self.G[_from].append(self.Edge(_to, _cost))
              self.G[_to].append(self.Edge(_from, _cost))
              self._E += 2
  
          def shortest_path(self, s):  # ,g):
              # 始点sから頂点iまでの最短経路長のリストを返す
  
              que = []  # priority queue
              d = [inf] * self.V()
              prev = [
                  None
              ] * self.V()  # prev[j]は，sからjへ最短経路で行くときのjの一つ前の場所
              d[s] = 0
              heapq.heappush(que, (0, s))  # 始点の距離と頂点番号をヒープに追加
  
              while len(que) != 0:
                  # キューに格納されてある中で一番コストが小さい頂点を取り出す
                  cost, v = heapq.heappop(que)
  
                  # キューに格納された最短経路長候補がdの距離よりも大きい場合に処理をスキップ
                  if d[v] < cost:
                      continue
  
                  # 頂点vに隣接する各頂点iに対して，vを経由した場合の距離を計算して，これがd[i]よりも小さい場合に更新
                  for i in range(len(self.G[v])):
                      e = self.G[v][i]  # vのi個目の隣接辺
                      if d[e.to] > d[v] + e.cost:
                          d[e.to] = d[v] + e.cost  # 更新
                          prev[e.to] = v
                          heapq.heappush(
                              que, (d[e.to], e.to)
                          )  # queに新たな最短経路長候補を追加
  
              """#sからgまでの最短経路
              path = []
              pos = g #今いる場所，ゴールで初期化
              for _ in range(self.V()+1):
                  path.append(pos)
                  if pos == s:
                      break
                  #print("pos:",format(pos))
                  pos = prev[pos]
              path.reverse()
              #print(path)"""
  
              return d  # ,path
  
      ########################
  
      djk = Dijkstra(N)
  
      for i in range(M):
          l, r, c = MI()
          l -= 1
          r -= 1
          djk.add(l, r, c)
  
      for i in range(1, N):
          djk.add(i, i - 1, 0)
  
      d = djk.shortest_path(0)
      ans = d[-1]
  
      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"}]