#!/usr/bin/env python3
import sys
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import csgraph_from_dense
from scipy.sparse.csgraph import dijkstra

def solve(N: int, M: int, L: "List[int]", R: "List[int]", C: "List[int]"):
    matrix = [[float('inf')]*N for _ in range(N)]

    for i in range(M):
        matrix[L[i]-1][R[i]-1] = C[i]

    for i in range(N-1):
        matrix[i+1][i]=0

    csr = csgraph_from_dense(matrix, null_value=float('inf'))
    dist_matrix = dijkstra(csr,directed=True,indices=0)

    if dist_matrix[-1] == float('inf'):
        print(-1)
    else:
        print(int(dist_matrix[-1]))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    L = [int()] * (M)  # type: "List[int]"
    R = [int()] * (M)  # type: "List[int]"
    C = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        L[i] = int(next(tokens))
        R[i] = int(next(tokens))
        C[i] = int(next(tokens))
    solve(N, M, L, R, C)

if __name__ == '__main__':
    main()
