import bisect
import sys
input = sys.stdin.readline


n, m = map(int, input().split())
edges = []
for _ in range(m):
    l, r, c = map(int, input().split())
    edges.append([l-1, r-1, c])

edges.sort()

DP = [[0, 0]]
#DP = [float('INF')] * n
#DP[0] = 0

#for l, r, c in edges:
#    for i in range(l+1, r+1):
#        DP[i] = min(DP[i], DP[l]+c)

for l, r, c in edges:
    idx = bisect.bisect_left(DP, [l, 0])
    if idx == len(DP):
        print(-1)
        exit()
    dist = DP[idx][1] + c
    nidx = bisect.bisect_left(DP, [r, 0])
    if len(DP) == nidx:
        DP.append([r, dist])
    elif DP[nidx][0] == r:
        if dist < DP[nidx][1]:
            DP[nidx][1] = dist
    else:
        DP.insert(nidx, [r, dist])

v, d = DP[-1]
if v == n-1:
    print(d)
else:
    print(-1)