import sys,queue,math,copy,itertools,bisect,collections,heapq

S = set()
curmin = 10 ** 10  # 現時点での最小値
flag = False  # 更新が必要か

def insert(x):
    global curmin, flag
    S.add(x)
    if x <= curmin:
        curmin = x
        flag = False
    return


def erase(x):
    global curmin, flag
    S.remove(x)
    if x == curmin:
        flag = True
    return


def minimum():
    global curmin, flag
    if flag:
        curmin = min(S)
        flag = False
    return curmin

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]



    N,M = LI()
    p = [LI() for _ in range(M)]

    p.sort()

    r = [[] for _ in range(N+1)]
    d = 0
    j = 0
    for i in range(1,N+1):
        while j < M and p[j][0] == i:
            insert(d + p[j][2])
            r[p[j][1]].append(d + p[j][2])
            j += 1
        for z in r[i]:
            erase(z)
        if i == N: break
        if len(S) == 0:
            print(-1)
            return

        d = minimum()
    print(d)

if __name__ == '__main__':
    main()