import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():

    N,M=MI()
    
    #############
    INF = 10**16

    LV = (N-1).bit_length()
    N0 = 2**LV
    data = [INF]*(2*N0)
    lazy = [None]*(2*N0)

    # 伝搬対象の区間を求める
    def gindex(l, r):
        L = (l + N0) >> 1; R = (r + N0) >> 1
        lc = 0 if l & 1 else (L & -L).bit_length()
        rc = 0 if r & 1 else (R & -R).bit_length()
        for i in range(LV):
            if rc <= i:
                yield R
            if L < R and lc <= i:
                yield L
            L >>= 1; R >>= 1

    # 遅延伝搬処理
    def propagates(*ids):
        for i in reversed(ids):
            v = lazy[i-1]
            if v is None:
                continue
            lazy[2*i-1] = data[2*i-1] = lazy[2*i] = data[2*i] = v
            lazy[i-1] = None

    # 区間[l, r)をxで更新
    def update(l, r, x):
        *ids, = gindex(l, r)
        propagates(*ids)

        L = N0 + l; R = N0 + r
        while L < R:
            if R & 1:
                R -= 1
                lazy[R-1] = data[R-1] = x
            if L & 1:
                lazy[L-1] = data[L-1] = x
                L += 1
            L >>= 1; R >>= 1
        for i in ids:
            data[i-1] = min(data[2*i-1], data[2*i])

    # 区間[l, r)内の最小値を求める
    def query(l, r):
        propagates(*gindex(l, r))
        L = N0 + l; R = N0 + r

        s = INF
        while L < R:
            if R & 1:
                R -= 1
                s = min(s, data[R-1])
            if L & 1:
                s = min(s, data[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s
    #############
    
    L=[0]*M
    R=[0]*M
    C=[0]*M
    for i in range(M):
        l,r,c=MI()
        l-=1
        r-=1
        L[i]=l
        R[i]=r
        C[i]=c
        
    R,C,L=zip(*sorted(zip(R,C,L)))
        
    
    
    #rが小さいものから見て(rが同じならcが小さい)確定させていく
    now=0#確定させたとこと
    update(0,1,0)
    
    for i in range(M):
        r=R[i]
        l=L[i]
        c=C[i]
        m=query(l,now+1)#移動もとの範囲での最小値
        update(now+1,r+1,m+c)
        now=r
        
        #このままだと入力例3みたいに，コストは低いけどl,rがちかいみたいなのを先に処理してしまう
        #とりあえず右端だけ更新しとけばokか?
        
        if m+c<query(now,now+1):
            update(now,now+1,m+c)
        
        #print((l+1,r+1,c),now+1,m,m+c)
        
    ans=query(N-1,N)
    if ans>=INF:
        print(-1)
    else:
        print(ans)
    
        
    
        
        
    

main()
