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):
  act = np.argmax(active)
  use = (l <= act) * (r > act)
  dp[r] = np.minimum(np.min(dp[r[use]] + c[use]), dp[r])
  active[act] = False
  
if dp[-1] > c.sum():
  print(-1)
else:
  print(dp[-1])