import numpy as np

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)

tail = np.array(tail, dtype=np.int)
head = np.array(head, dtype=np.int)
cost = np.array(cost, dtype=np.int)

inf = cost.sum() + 1
dp = np.ones(n, dtype=np.int) * inf
dp[0] = 0
act = 0
unexplr = np.ones(n, dtype=np.bool)
unexplr[0] = False
while unexplr[-1]:
  edge = (tail == act)
  dp[head[edge]] = np.minimum(dp[head[edge]], dp[act] + cost[edge])
  tmp = np.ones(n, dtype=np.int) * inf
  tmp[unexplr] = dp[unexplr]
  act = tmp.argmin()
  unexplr[act] = False
if dp[-1] < inf:
  print(dp[-1])
else:
  print(-1)