import sys
input = sys.stdin.readline
from scipy.sparse.csgraph import csgraph_from_dense,dijkstra

def main():
    N,M = map(int,input().split())
    edge = [[-1 for _ in range(N)] for _ in range(N)]
    for i in range(N-1):
        edge[i+1][i] = 0
    for _ in range(M):
        l,r,c = map(int,input().split())
        edge[l-1][r-1] = c

    G = csgraph_from_dense(edge,null_value = -1)
    if dijkstra(G,indices = 0)[-1] == float("inf"):
        print(-1)
    else:
        print(int(dijkstra(G,indices = 0)[-1]))

if __name__ == "__main__":
    main()
