

# shortest path on line

n, m = map(int, input().split())

graph = {i: {} for i in range(1, n + 1)}

for i in range(1, n):
    graph[i + 1][i] = 0

for _ in range(m):
    l, r, c = map(int, input().split())
    graph[l][r] = c
    graph[r][l] = c

###############################################################

import heapq

def dijkstra(s, graph, n):
    # ｓからのグラフの最短最短距離をまとめたデータを返す
    dist = [10**20 for i in range(n + 1)]
    dist[s] = 0
    pq = []
    heapq.heapify(pq)
    # 全ての接点を一度優先度付きキューに挿入する
    heapq.heappush(pq, (0, s))
    while pq:
        x = heapq.heappop(pq)
        node, mini_dis = x[1], x[0]
        for point in graph[node].keys():
            w = graph[node][point]
            # nodeからpointに対して伸びる辺の長さ
            newlen = dist[node] + w
            if newlen < dist[point]:
                heapq.heappush(pq, (newlen, point))
                dist[point] = newlen

    return dist
###############################################################


a = dijkstra(1, graph, n)
if a[n]>10**19:
    print(-1)
else:
    print(a[n])
    