import heapq

class Dijkstra:
    # heapqのimportを忘れずに

    def __init__(self, edge, start_point, goal_point=None):
        self.edge = edge
        self.start_point = start_point
        self.goal_point = goal_point

    def get_path(self, goal, prev):
        # ゴールまでの最短経路を求める
        path = [goal]
        dest = goal

        while prev[dest] != float("inf"):
            path.append(prev[dest])
            dest = prev[dest]

        return list(reversed(path))

    def execute(self):
        num_of_edge = len(self.edge)
        dist = [float("inf") for _ in range(num_of_edge)]
        prev = [float("inf") for _ in range(num_of_edge)]

        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

            for dest, cost in self.edge[src].items():
                if cost != float("inf") and dist[dest] > dist[src] + cost:
                    dist[dest] = dist[src] + cost
                    heapq.heappush(heap_q, (dist[dest], dest))
                    prev[dest] = src

        if self.goal_point is not None:
            return self.get_path(self.goal_point, prev)
        else:
            return dist

n,m = map(int,input().split())
data = [{} for _ in range(n)]
for i in range(1,n):
    data[i][i-1] = 0

for i in range(m):
    l,r,c = map(int,input().split())
    l -= 1
    r -= 1
    data[l][r] = c
#print(data)
dk =Dijkstra(data,0)
ans = dk.execute()

print(ans[n-1] if ans[n-1] < float("inf") else -1)
