import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

n,m=LI()
edge=[[] for _ in range(n)]

def dijkstra_heap(n,s,edge):
  #始点sから各頂点への最短距離
  d=[inf]*n
  used=[True]*n #True:未確定
  d[s]=0
  used[s]=False
  edgelist=[]
  for a,b in edge[s]:
    heapq.heappush(edgelist,a*(10**6)+b)
  while len(edgelist):
    minedge=heapq.heappop(edgelist)
    #まだ使われてない頂点の中から最小の距離のものを探す
    if not used[minedge%(10**6)]:
      continue
    v=minedge%(10**6)
    d[v]=minedge//(10**6)
    used[v]=False
    for e in edge[v]:
      if used[e[1]]:
        heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
  return d

for i in range(m):
  l,r,c=LI()
  for i in range(l-1,r-1):
    for j in range(i+1,r):
      edge[i].append([c,j])
      edge[j].append([c,i])

ans=dijkstra_heap(n,0,edge)
if ans[-1]==inf:
  print(-1)
  exit()
print(ans[-1])

