import heapq


def main():
    n, m = map(int, input().split())
    lrc = [list(map(int, input().split())) for _ in range(m)]

    adj = [[(0, i-1)] for i in range(n)]
    adj[0] = []

    for l, r, c in lrc:
        l -= 1
        r -= 1
        adj[l].append((c, r))

    WHITE = 0
    GRAY = 1
    BLACK = 2

    def dijkstra(s):
        colors = [WHITE] * n
        ds = [float("inf")] * n
        ps = [-1] * n
        pq = []

        ds[s] = 0
        ps[s] = -1
        heapq.heappush(pq, (0, s))

        while pq:
            d, u = heapq.heappop(pq)
            colors[u] = BLACK

            if ds[u] < d:
                continue

            for c, v in adj[u]:
                if colors[v] != BLACK:
                    if ds[u] + c < ds[v]:
                        ds[v] = ds[u] + c
                        ps[v] = u
                        colors[v] = GRAY
                        heapq.heappush(pq, (ds[v], v))

        return ds, ps

    ds, ps = dijkstra(0)

    ans = ds[-1]
    if ans == float("inf"):
        ans = -1

    print(ans)


if __name__ == "__main__":
    main()
