import heapq
  
class Dijkstra:
 
    def __init__(self, rote_map, start_point, goal_point=None):
        self.rote_map = rote_map
        self.start_point = start_point
        self.goal_point = goal_point
 
    def execute(self):
        num_of_city = len(self.rote_map)
        dist = [None for i in range(num_of_city)]
 		
        dist[self.start_point] = 0
        heap_q = []
        heapq.heappush(heap_q, (0, self.start_point))
        while len(heap_q) != 0:
            prev_cost, src = heapq.heappop(heap_q)
            if dist[src] < prev_cost:
                continue
            if src == self.goal_point:
                return dist[self.goal_point]
 
            for dest, cost in self.rote_map[src].items():
                if dist[dest] == None or dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
        return -1
 
n,m=map(int, input().split()) 
route_map = [dict() for i in range(n)]
 
for i in range(m):
    u, v, a = map(int, input().split())
    route_map[u-1][v-1] = a
    
for i in range(n-1):
    route_map[i+1][i] = 0

# ゴールまでの最短経路
print(Dijkstra(route_map,0,n-1).execute())
