N, M = map(int, input().split())
LRC = [list(map(int, input().split())) for _ in range(M)]

LRC.sort(key=lambda x: (x[0], x[1]))
# print(LRC)

dp = [float('inf')] * (N + 1)
dp[1] = 0

sml, big = LRC[0][0], LRC[0][1]

for i in range(1, M):
    l, r, c = LRC[i]
    if l <= big:
        if big <= r:
            big = r
    else:
        print(-1)
        exit()
if sml != 1 or big != N:
    print(0)
    exit()



for lrc in LRC:
    l, r, c = lrc
    for i in range(l, r + 1):
        dp[i] = min(dp[i], dp[l] + c)
print(dp[-1])
