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 = [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)

ans = DP[n-1]
if ans == float('INF'):
    ans = -1
print(ans)
