import bisect
N, M = map(int, input().split())
edge = []
for i in range(M):
    a, b, c = map(int, input().split())
    edge.append((a-1, b-1, c))

edge = sorted(edge)
DEKAI = 10 ** 18
dp = [DEKAI] * N
dp[0] = 0
min_L = 0
B = []
for a, b, c in edge:
    if dp[a] == DEKAI:
        idx = bisect.bisect_left(B, a+1)
        if idx > len(B) - 1:
            break
        dp[b] = min(dp[b], dp[B[idx]] + c) 
    else:
        dp[b] = min(dp[b], dp[a] + c)
    B.append(b)

if dp[N-1] == DEKAI:
    print(-1)
else:
    print(dp[N-1])

