import sys
n,m=map(int,input().split())

edges=[]
edge=[]
v=[]
rss=[0]
inf=float('inf')

for i in range(m):
    l,r,c=map(int,input().split())
    edges.append((l-1,r-1,c))
    #rs.append(l-1)
    rss.append(r-1)
edges.append((n-1,n-1,0))
edges.sort()
rss.sort()
rs=[rss[0]]
for i in range(m):
    if edges[i][0]==edges[i+1][0] and edges[i][1]<=edges[i+1][1] and edges[i][2]>=edges[i+1][2]:
        continue
    edge.append(edges[i])
for i in range(1,m+1):
    #print(rs)
    if rs[-1]!=rss[i]:
        rs.append(rss[i])
dp=[inf]*(len(rs))
dp[0]=0
if rs[-1]!=n-1:
    print(-1)
    sys.exit()
import bisect
for i in range(len(edge)):
    l,r,c=edge[i]
    idx=bisect.bisect_left(rs,r)
    idx2=bisect.bisect_left(rs,l)
    dp[idx]=min(dp[idx],dp[idx2]+c)
print(dp[-1] if dp[-1]!=inf else -1)







