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

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

for i in range(m):
    l,r,c=map(int,input().split())
    edge.append((l-1,r-1,c))
    #rs.append(l-1)
    rss.append(r-1)

edge.sort()
rss.sort()
rs=[rss[0]]
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

import bisect
for i in range(m):
    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)
