import sys
input = sys.stdin.readline


def main():
    N, M = map(int, input().split())
    E = []
    for _ in range(M):
        l, r, c = map(int, input().split())
        E.append((l-1, r-1, c))

    dp = [float("inf")]*N
    dp[0] = 0

    for l, r, c in sorted(E):
        for i in range(r, l, -1):
            if dp[i] > dp[l] + c:
                dp[i] = dp[l] + c
            else:
                break
    print(-1 if dp[-1] == float("inf") else dp[-1])


if __name__ == '__main__':
    main()
