from heapq import heappop, heappush
N, M = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
    
edges = X[:]
for i in range(1, N):
    edges.append([i + 1, i, 0])
    
INF = 10 ** 9 + 7
d = [INF] * N
d[0] = 0

visited = [False] * N
visited[0] = True

# Priority queue (cost, node)
pq = []
heappush(pq, (0, 0))

while len(pq) > 0:
    cost, u = heappop(pq)
    visited[u] = True
    
    # Search nodes connected with target node 'u'
    for v, w, c in edges:            
        node = w - 1
        if v - 1 != u or visited[node]:
            continue

        if d[node] > d[u] + c:
            d[node] = d[u] + c
            heappush(pq, (d[node], node))
                   
print(d[-1] if d[-1] != INF else -1)
