n, m = map(int, input().split())
tail = []
head = []
cost = []
for _ in range(m):
  l, r, c = map(int, input().split())
  tail.append(l - 1)
  head.append(r - 1)
  cost.append(c)
tail += list(range(1, n))
head += list(range(n - 1))
cost += [0] * (n - 1)

inf = sum(cost) + 1
dp = [0] + [inf] * (n - 1)
act = 0
unexplr = [False] + [True] * (n - 1)
while unexplr[-1]:
  for edge in range(m + n - 1):
    to = head[edge]
    if act == tail[edge] and unexplr[to] and dp[to] > dp[act] + cost[to]:
      dp[to] = dp[act] + cost[to]
  short = inf
  for vertex in range(n):
    if unexplr[vertex] and dp[vertex] < short:
      act = vertex
      short = dp[vertex]
  unexplr[act] = False
if dp[-1] < inf:
  print(dp[-1])
else:
  print(-1)