import heapq

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 = 1 << 60
dp = [0] + [inf] * (n - 1)
queue = [(0, 0)]
check = [False] * n
while queue:
  _, act = heapq.heappop(queue)
  check[act] = True
  for edge in range(n + m - 1):
    if tail[edge] != act:
      continue
    vertex = head[edge]
    if check[vertex]:
      continue
    dist = dp[act] + cost[edge]
    if dp[vertex] > dist:
      heapq.heappush(queue, (dist, vertex))
      dp[vertex] = dist
if dp[-1] < inf:
  print(dp[-1])
else:
  print(-1)