import scala.collection.mutable.{Set => MSet, ArrayBuffer}
import scala.io.StdIn._

object Main extends App {
  val n = readInt
  val players = ArrayBuffer.fill(n)(readLine.split(" ").map(_.toInt - 1).toList)
  val validPlayers = MSet((0 until n): _*)

  @scala.annotation.tailrec
  def play(days: Int): Int = {
    var noPlay = true
    val played = new Array[Boolean](n)
    validPlayers.foreach { p =>
      if (players(p).nonEmpty) {
        val oppo = players(p).head
        def play(i: Int): Unit = {
          players(i) = players(i).tail
          played(i) = true
          if (players(i).isEmpty) validPlayers -= i
        }
        if (players(oppo).head == p && !played(p) && !played(oppo)) {
          play(p)
          play(oppo)
          noPlay = false
        }
      }
    }
    if (noPlay) {
      if (players.exists(_.nonEmpty)) -1 else days
    } else play(days + 1)
  }

  println(play(0))
}