N,M = map(int,input().split())
Glaph = [[0 for j in range(N)] for i in range(N)]
for i in range(M):
  l,r,c = map(int,input().split())
  l -= 1
  r -= 1
  for s in range(l,r+1):
    for t in range(l,r+1):
      if s != t:
        if Glaph[s][t] == 0 or Glaph[s][t] > c:
          Glaph[s][t] = c

def djikstra(s,g,Glaph):
  INF = 1000000
  done = [ 0  for i in range(N)]
  cost = [INF for i in range(N)]
    
  done[s] = 1
  cost[s] = 0
  done_node = s
  
  for i in range(N):
    min = INF
   
    k = done_node 
    for j in range(N):
      if done[j] == 0:
        
        if Glaph[k][j] != 0 and cost[j] > cost[k] + Glaph[k][j]:
          cost[j] = cost[k] + Glaph[k][j]
        
        if min > cost[j]:
          min = cost[j]
          done_node = j
     
    done[done_node] = 1
    if done_node == g:
      break
      
  if cost[g] == INF:
    return(-1)
  else:
    return cost[g]
      
print(djikstra(0,N-1,Glaph))