
n, m = [ int(v) for v in input().split() ]
edge = sorted([ [ int(v) for v in input().split() ] for i in range(m) ])
inf = 10**12
ln = len(bin(n-1))-1
ln2 = 2**(ln-1)
segtree = [ inf for i in range(2**ln-1) ]


def update_tomin(p, q):
    p += 2**(ln-1)-2
    r = 0
    while p >= 0:
        segtree[p] = min(segtree[p], q)
        p = (p-1)//2

def getmin(a,b,k=0,l=0,r=ln2):
    if r <= a or b <= l:
        return inf
    elif l == r:
        return inf
    elif a == l and b == r:
        return segtree[k]
    elif l + 1 == r:
        return segtree[k]
    else:
        return min( getmin(a,min(b,(l+r)//2),2*k+1,l,(l+r)//2), getmin(max(a,(l+r)//2),b,2*k+2,(l+r)//2,r) )

update_tomin(1, 0)
for i in edge:
    l, r, c = i
    mincost = getmin(l-1,r)
    update_tomin(r, c+mincost)
print(getmin(n-1,n))