from heapq import heappush, heappop

def dijkstra(edges, size, source):
    # edge=探索するグラフ
    # size=グラフの点の数
    # source=スタート点
    distance = [float('inf')] * size
    distance[source] = 0
    visited = [False] * size
    pq = []
    heappush(pq, (0, source))
    while pq:
        dist_v, v = heappop(pq)
        visited[v] = True
        for u, weight in edges[v]:
            if not visited[u]:
                new_dist = dist_v + weight
                if distance[u] > new_dist:
                    distance[u] = new_dist
                    heappush(pq, (new_dist, u))
    return distance[n-1]

n,m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(m)]
b = [[] for i in range(n)]

for i in range(m):
    b[a[i][0]-1].append((a[i][1]-1,a[i][2]))

# 戻るルートをコストゼロで追加
for i in range(n-1):
    b[i+1].append((i,0))

x=dijkstra(b,n,0)

if x==float('inf'):
    print(-1)

else:
    print(x)