import heapq

n, m = map(int, input().split(' '))
edges = [[] for _ in range(n)]
for _ in range(m):
  l, r, c = map(int, input().split(' '))
  edges[l - 1].append((r - 1, c))

dists = [None for _ in range(n)]
h = [(0, 0)]
for i in range(n):
  while len(h) > 0 and h[0][1] < i:
    heapq.heappop(h)
  if len(h) == 0:
    break
  dists[i] = h[0][0]
  for e in edges[i]:
    heapq.heappush(h, (e[1] + dists[i], e[0]))
print(-1 if dists[i] is None else dists[-1])