import heapq

N, M = map(int, input().split())
INF = 10**9
g = [[INF for _ in range(N+1)] for _ in range(N+1)]
for _ in range(M):
    L, R, C = map(int, input().split())
    for i in range(L, R+1):
        for j in range(L, R+1):
            if i == j:
                g[i][j] = 0
            else:
                g[i][j] = min(g[i][j], C)

def dijkstra(adj, s):
    num = len(adj)
    dist = [INF for _ in range(num)]
    prev = [INF for _ in range(num)]
    dist[s] = 0
    q = []
    heapq.heappush(q, (0, s))
    while len(q) != 0:
        prov_cost, src = heapq.heappop(q)
        if dist[src] < prov_cost:
            continue
        for dest in range(num):
            cost = adj[src][dest]
            if cost != INF and dist[dest] > dist[src] + cost:
                dist[dest] = dist[src] + cost
                heapq.heappush(q, (dist[dest], dest))
                prev[dest] = src
    return dist

ans = dijkstra(g, 1)[-1]
if ans == INF:
    print(-1)
else:
    print(ans)