import heapq

def main():
    n, m = map(int, input().split())
    dic = {}
    for _ in range(m):
        l, r, c = map(int, input().split())
        ky = (l-1, r-1)
        if ky in dic:
            if c < dic[ky]:
                dic[ky] = c
        else:
            dic[ky] = c
    dist = [float("inf")]*n
    dist[0] = 0
    hq = []
    heapq.heappush(hq, [0, 0])
    while len(hq) > 0:
        v, p = heapq.heappop(hq)
        if v > dist[p]:
            continue
        if p == n-1:
            break
        rm = set()
        for l, r in dic.keys():
            u = dic[(l, r)]
            if l <= p <= r:
                rm.add((l, r))
                for i in range(l, r+1):
                    if i == p:
                        continue
                    if dist[i] > dist[p] + u:
                        dist[i] = dist[p] + u
                        heapq.heappush(hq, [dist[i], i])
        for ky in rm:
            dic.pop(ky)
    if dist[n-1] == float("inf"):
        print(-1)
    else:
        print(dist[n-1])

if __name__ == "__main__":
    main()
