import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val islands = Array.tabulate(n) { _ ⇒ Nil: List[(Int, Int)]}
  for (_ ← 0 until m){
    val Array(a, b, c) = readLine.trim.split(' ').map(_.toInt)
    islands(a - 1) ::= (b - 1, c)
  }
  val minTime = Array.tabulate(n){_ ⇒ Int.MaxValue}.also{ar ⇒ ar(0) = 0}
  val queue = mutable.PriorityQueue()(Ordering.by[(Int, Int), Int](_._2).reverse).also{q ⇒ q.enqueue((0, 0))}
  while (queue.nonEmpty){
    val (current, time) = queue.dequeue()
    if (minTime(current) == time && current != n - 1){
      for ((next, c) ← islands(current)) {
        if (time <= c && minTime(next) > c) {
          minTime(next) = c
          queue.enqueue((next, c))
        }
      }
    }
  }

  var lastTime = -1
  for (from ← islands.indices){
    islands(from).filter(_._1 == n - 1).foreach{
      case (_, c) if minTime(from) <= c && lastTime < c ⇒ lastTime = c
      case _ ⇒
    }
  }
  println(lastTime)
  implicit class Extend[T](value: T){
    def also(func: T ⇒ Unit): T = {func(value); value}
    def let[A](func: T ⇒ A): A = func(value)
  }
}
