N,M = map(int,input().split())
LRC = [list(map(int,input().split())) for _ in range(M)]

def dijkstra(s,n,cost):
  #始点sから各頂点への最短距離
  #n:頂点数,　cost[u][v] : 辺uvのコスト(存在しないときはinf)
  d = [float("inf")] * n
  used = [False] * n
  d[s] = 0

  while True:
    v = -1
    #まだ使われてない頂点の中から最小の距離のものを探す
    for i in range(n):
      if (not used[i]) and (v == -1):
        v = i
      elif (not used[i]) and d[i] < d[v]:
        v = i
    if v == -1:
      break
    used[v] = True

    for j in range(n):
      d[j] = min(d[j],d[v]+cost[v][j])
  return d

INF = float('inf')
G = [[INF for _ in range(N)] for _ in range(N)]

for i in range(N-1):
  G[i+1][i] = 0
for l,r,c in LRC:
  l -= 1
  r -= 1
  G[l][r] = c
#print(G)

#print(dijkstra(0,N,G))
p = dijkstra(0,N,G)[N-1]
print(p if p != INF else -1)