import numpy as np
import heapq

INF=10**12
# def daik(s,g,d):
#     used=np.array([])
#     heap=[]
#     table=np.array([INF for i in range(N)])
#     table[s]=0
#     heapq.heappush(heap,(table[s],s))
#     used=np.append(used,s)
                
#     while heap and not(g in used):
#         q=heapq.heappop(heap)
#         a=q[1]
#         used=np.append(used,a)
#         for i in range(N):
#             if not(i in used):
#                 cost=d[a,i] if a<=i else d[i,a]
#                 if cost!=INF:
#                     table[i]=min(table[i],table[a]+cost)
#                     heapq.heappush(heap,(table[i],i))
#     return table[g]

def daik(s,g,d):
    used=np.array([])
    heap=[]
    table=np.array([INF for i in range(N)])
    table[s]=0
    heapq.heappush(heap,(table[s],s))
    used=np.append(used,s)
                
    while heap and not(g in used):
        q=heapq.heappop(heap)
        a=q[1]
        used=np.append(used,a)
        for i in range(N):
            if not(i in used):
                cost=d[a,i] 
                if cost!=INF:
                    table[i]=min(table[i],table[a]+cost)
                    heapq.heappush(heap,(table[i],i))
    return table[g]

N,M=map(int,input().split())
LRC=[]
for i in range(M):
    L,R,C=map(int,input().split())
    LRC.append([L-1,R-1,C])
LRC=np.array(LRC)
LRC=LRC[np.argsort(LRC[:,2])][::-1]

d=np.array([[INF for j in range(N)] for i in range(N)])
# for i in range(N):
#     d[i,i]=0
# l=np.array([i+1 for i in range(N)])
# l[-1]=N
# for m in range(M):
#     L,R,C=LRC[m,0],LRC[m,1],LRC[m,2]
#     for i in range(L,R+1):
#         if l[i]<N:
#             d[i,l[i]:R+1]=C
#             l[i]=R+1
for i in range(N-1):
    d[i+1,i]=0
for i in range(M):
    C=LRC[i,2]
    d[LRC[i,0],LRC[i,1]]=C
    d[LRC[i,1],LRC[i,0]]=C
ans=daik(0,N-1,d)
if ans==INF:
    print(-1)
else:
    print(ans)