from sys import stdout
import heapq
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def dumpheap(h):
    a = []
    print("- -")
    while len(h)>0:
        z = heapq.heappop(h)
        print(z)
        a.append(z)
    for z in a:
        heapq.heappush(h,z)

n,m = inm()
a = []
for i in range(m):
    l,r,c = inm()
    a.append((l,r,c))
a.sort()
if a[0][0]>1:
    print(-1)
    exit()

ddprint(a)

h = []
top = 0
for i in range(m):
    l,r,c = a[i]
    if l==1:
        heapq.heappush(h,(c,0,l,r,c))
    else:
        top = i
        break

if DBG:
    dumpheap(h)

curld,curd,curl,curr,curc = heapq.heappop(h)
for i in range(top,m):
    l,r,c = a[i]
    while curr<l:
        if len(h)==0:
            print(-1)
            exit()
        curld,curd,curl,curr,curc = heapq.heappop(h)
    heapq.heappush(h,(curld,curd,curl,curr,curc))
    #ddprint("push {} {} {} {} {}".format(curld+c,curld,l,r,c))
    heapq.heappush(h,(curld+c,curld,l,r,c))

r=0
while r<n:
    if len(h)==0:
            print(-1)
            exit()
    ld,d,l,r,c = heapq.heappop(h)
print(ld)
