def main():
    from sys import stdin
    input = stdin.readline
    import numpy as np
    import scipy.sparse as sp
    n, m = map(int, input().split())
    abc = np.array([list(map(int, input().split())) for _ in [0]*m])
    data = np.append(abc[:, 2], np.zeros(n-1))
    row = np.append(abc[:, 0]-1, np.arange(1, n))
    col = np.append(abc[:, 1]-1, np.arange(n-1))
    matrix = sp.csr_matrix((data, (row, col)), shape=(n, n), dtype=np.int32)
    d = sp.csgraph.dijkstra(matrix, indices=0)
    print(int(d[n-1]))


main()