import sys
from collections import defaultdict as dd
from bisect import bisect_left as bl
import heapq
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
e = dd(list)
s = []
for _ in range(M):
  a.append(list(map(int, input().split())))
  e[a[-1][0]].append((a[-1][1], a[-1][2]))
  s.append(a[-1][0])
a.sort(key = lambda x: (x[0], x[1]))
s.sort()
x = 0
#print(s)
for i in range(M):
  y = bl(s, a[i][1])
  #print(a[i], x, y)
  for j in range(max(0, x - 1), min(y, N + 1)):
    e[a[i][1]].append((a[j][0], 0))
  x = y
class dijkstra:
  def __init__(self, n, e):
    self.e = e
    self.n = n
  def path(self, s, t):
    d = [float("inf")] * (self.n + 1)
    vis = set()
    d[s] = 0
    h = [s]
    while not t in vis and len(h):
      v = heapq.heappop(h)
      v1 = v % (10 ** 6)
      v0 = v // (10 ** 6)
      if v1 in vis: continue
      vis.add(v1)
      for p in self.e[v1]:
        d[p[0]] = min(d[p[0]], d[v1] + p[1])
        if p[0] in vis: continue
        heapq.heappush(h, d[p[0]] * (10 ** 6) + p[0])
    return d[t]
d = dijkstra(N, e)
res = d.path(1, N)
if res == float("inf"): print(-1)
else: print(res)