n, m = map(int, input().split())
route_list = [[float("inf")]*n for _ in range(n)]
lrc = [ list(map(int, input().split())) for _ in range(m) ]
for l, r, c in lrc:
  for i in range(l, r):
    for j in range(i+1, r+1):
      route_list[i-1][j-1] = min(route_list[i-1][j-1], c)
      route_list[j-1][i-1] = min(route_list[j-1][i-1], c)
  
  
node_num = n

unsearched_nodes = list(range(node_num))
distance = [float("inf")] * node_num
previous_nodes = [-1] * node_num
distance[0] = 0 

def get_target_min_index(min_index, distance, unsearched_nodes):
    start = 0
    while True:
        index = distance.index(min_index, start)
        found = index in unsearched_nodes
        if found:
            return index
        else:
            start = index + 1
            
  
while(len(unsearched_nodes) != 0):
   
    posible_min_distance = float("inf")
    for node_index in unsearched_nodes: 
        if posible_min_distance > distance[node_index]: 
            posible_min_distance = distance[node_index]
    target_min_index = get_target_min_index(posible_min_distance, distance, unsearched_nodes) 
    unsearched_nodes.remove(target_min_index) 

    target_edge = route_list[target_min_index] 
    for index, route_dis in enumerate(target_edge):
        if route_dis != 0:
            if distance[index] > (distance[ target_min_index] + route_dis):
                distance[index] = distance[ target_min_index] + route_dis 
                previous_nodes[index] =  target_min_index 
                
if distance[node_num - 1] == float("inf"):
  print(-1)
else:
  print(distance[node_num - 1])