import numpy as np

n, m = map(int, input().split())
edge_l = []
edge_r = []
edge_c = []
for _ in range(m):
  l, r, c = map(int, input().split())
  edge_l.append(l - 1)
  edge_r.append(r - 1)
  edge_c.append(c)
l = np.array(edge_l, dtype=np.int)
r = np.array(edge_r, dtype=np.int)
c = np.array(edge_c, dtype=np.int)

dp = np.ones(n, dtype=np.int) * (c.sum() + 1)
dp[0] = 0
active = np.zeros(n, dtype=np.bool)
active[0] = True
while np.any(active[:-1]):
  act = active.argmax()
  use = (l <= act) * (r > act)
  dp[r[use]] = np.minimum(dp[act] + c[use], dp[r[use]])
  active[r[use]] = True
if active[-1]:
  print(dp[-1])
else:
  print(-1)