import heapq
WHITE = 0
GRAY = 1
BLACK = 2
INFTY = 10**16
def dijkstra(s, VC):
    color = [WHITE for i in range(N)]
    d = [INFTY for i in range(N)]
    d[s] = 0
    pq = []
    heapq.heappush(pq, (0, s))

    while len(pq) != 0:
        u = heapq.heappop(pq)[1]
        color[u] = BLACK

        for v, c in VC[u]:
            if color[v] != BLACK:
                if d[u] + c < d[v]:
                    d[v] = d[u] + c
                    color[v] = GRAY
                    heapq.heappush(pq, (d[v], v))

    return d

N, M = map(int, input().split())
V = [[] for i in range(N)]
for m in range(M):
    L, R, C = map(int, input().split())
    V[L-1].append([R-1, C])
    V[R-1].append([L-1, C])
for i in range(1, N):
    V[i].append([i-1, 0])

d = dijkstra(0, V)
ans = d[N-1]
if ans == INFTY:
    print(-1)
else:
    print(ans)