n, m = map(int, raw_input().split())
edges = []
for _ in range(m):
    edges.append(map(int, raw_input().split()))
edges.sort()

dist = [float('inf')] * (n + 1)
dist[1] = 0
for l, r, c in edges:
    for e in xrange(l + 1, r + 1):
        dist[e] = min(dist[l] + c, dist[e])

if dist[n] == float('inf'):
    print -1
else:
    print dist[n]