import heapq

INF = 2**62-1

def dijkstra(route_map, start_point):
    num_of_city = len(route_map)
    dist = [INF for _ in range(num_of_city)]

    dist[start_point] = 0
    heap_q = []
    heapq.heappush(heap_q, (0, start_point))
    while heap_q:
        prev_cost, src = heapq.heappop(heap_q)

        if dist[src] < prev_cost:
            continue

        for dest, cost in route_map[src].items():
            if dist[dest] > dist[src] + cost:
                dist[dest] = dist[src] + cost
                heapq.heappush(heap_q, (dist[dest], dest))
    return dist

n, m = list(map(int, input().split()))
route_map = [{} for _ in range(n)]

for i in range(m):
    u, v, a = list(map(int, input().split()))
    u, v = u - 1, v - 1
    route_map[u][v] = a

for i in range(n-1):
    u, v = i+1, i
    route_map[u][v] = 0

# ゴールまでの最短経路
dijkstra_result = dijkstra(route_map, 0)

if dijkstra_result[-1] != INF:
    print(dijkstra_result[-1])
else:
    print(-1)
