N, M = map(int, input().strip().split())
LRC = [list(map(int, input().strip().split())) for i in range(M)]
LRC.sort()
LRC = sorted(LRC, key=lambda h: (h[0], h[2]))
#print(LRC)

max_end = 1
tf = True
for lrc in LRC:
    if lrc[0] > max_end:
        tf = False
        break
    max_end = max(max_end, lrc[1])

if not(tf) or max_end < N:
    print(-1)
else:
    INF = -1
    dp = [INF] * (N+1)
    dp[1] = 0
    for lrc in LRC:
        for i in range(lrc[0], lrc[1]+1):
            if i == 0:
                continue
            if dp[i] == INF:
                dp[i] = dp[lrc[0]] + lrc[2]
            else:
                dp[i] = min(dp[lrc[0]] + lrc[2], dp[i])
    #print(dp)
    print(dp[N])
