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

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)]
    x = []
    d = 0
    j = 0
    for i in range(1,N+1):
        while j < M and p[j][0] == i:
            x.append(d + p[j][2])
            r[p[j][1]].append(d + p[j][2])
            j += 1
        for z in r[i]:
            x.remove(z)
        if i == N: break
        if x == []:
            print(-1)
            return
        else:
            d = min(x)
    print(d)

if __name__ == '__main__':
    main()