n, m = gets.chomp.split.map(&:to_i)
to = Array.new(n){[]}
from = Array.new(n){[0, 0]}
m.times{
    a, b, c = gets.chomp.split.map(&:to_i)
    to[a] << [b, c]
    from[b][0] += 1
}
arr = to[0]
while temp = arr.shift
    a, b = temp[0], temp[1]
    from[a][0] -= 1
    from[a][1] = b if from[a][1] < b
    to[a].each{|c, d| arr << [c, from[a][1] + d]} if from[a][0] == 0
end
puts from[n - 1][1]
