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)

  @scala.annotation.tailrec
  def play(days: Int): Int = {
    val played = MSet[Int]()
    (0 until n) foreach { p =>
      if (players(p).nonEmpty) {
        val oppo = players(p).head
        if (players(oppo).head == p && !played(p) && !played(oppo)) {
          players(p) = players(p).tail
          played += p
          players(oppo) = players(oppo).tail
          played += oppo
        }
      }
    }
    if (played.isEmpty) {
      if (players.exists(_.nonEmpty)) -1 else days
    } else play(days + 1)
  }

  println(play(0))
}