import heapq
import collections
def main():

    N, M = map(int, input().split())
    graph = collections.defaultdict(dict)
    for i in range(N-1):
        graph[i+1][i] = 0

    for _ in range(M):
        L, R, C = map(int, input().split())
        graph[L-1][R-1] = C
        graph[R-1][L-1] = C

    dist = [float('inf') for _ in range(N)]
    dist[0] = 0
    v = []
    heapq.heappush(v, [0, 0])
    while v:
        d, u = heapq.heappop(v)
        if d > dist[u]: continue
        for w in graph[u]:
            if dist[w] > d + graph[u][w]:
                dist[w] = d + graph[u][w]
                heapq.heappush(v, [dist[w], w])


    if dist[N-1] == float('inf'): return -1
    return dist[N-1]

if __name__ == '__main__':
    # main()
    print(main())