import networkx as nx

N, M = map(int, input().split())

G = nx.DiGraph()
G.add_nodes_from(range(1, N + 1))
for _ in range(M):
    l, r, c = map(int, input().split())
    G.add_edge(l, r, weight=c)
    for x in range(l, r):
        G.add_edge(x + 1, x, weight=0)

try:
    print(nx.dijkstra_path_length(G, 1, N))
except nx.NetworkXNoPath:
    print(-1)