import queue
N, M = map(int, input().split())
edge_list = [{} for i in range(N+1)]
min_distance = [float('inf')] * (N+1)
for i in range(M):
    L, R, C = map(int, input().split())
    edge_list[L][R] = C
    edge_list[R][L] = C
    for l in range(L+1,R+1):
        edge_list[l][l-1] = 0
        # edge_list[l-1][l] = 0
# print(edge_list)
def solve(s, t):
    fixed = {}
    q = queue.PriorityQueue()
    q.put((0, s))
    while q.empty() == False:
        w, x = q.get()
        # print(w, x)
        if x == t:
            return w
        if x in fixed:
            continue
        fixed[x] = w
        for y in edge_list[x]:
            if y not in fixed:
                q.put((w + edge_list[x][y], y))
        # print(fixed)
    return None

answer = solve(1, N)
if answer is None:
    print(-1)
else:
    print(answer)