import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val A = Array.ofDim[Int](N+1, N)
  for (i <- 1 to N) {
    for (k <- 1 until N) {
      A(i)(k) = in.next().toInt
    }
  }

  def hash(_a: Int, _b: Int): Int = {
    val a = _a min _b
    val b = _a max _b

    ((a-1) * (2*N - a)) / 2 + (b-a) - 1
  }

  // 
  val nodeNum = (N*(N-1)) / 2

  val inDegree = new Array[Int](nodeNum)

  val graph = Array.fill[List[Int]](nodeNum)(Nil)

  for (a <- 1 to N; k <- 2 until N) {
    val from = hash(a, A(a)(k-1))
    val b = A(a)(k)

    graph(from) =
      hash(a, b) :: graph(from)
    inDegree(hash(a, b)) += 1
  }


  val dp = new Array[Boolean](nodeNum)
  val ans = new Array[Int](nodeNum)
  val q = new Queue[(Int, Int)]()
  for (i <- 0 until nodeNum) {
    if (!dp(i) && inDegree(i) == 0) {
      q.enqueue((i, 1))
      while (!q.isEmpty) {
        val (now, cnt) = q.dequeue()

        ans(now) = Math.max(ans(now), cnt)
        if (inDegree(now) == 0) {
          dp(now) = true

          val nextCnt = ans(now) + 1
          for (e <- graph(now)) {
            inDegree(e) -= 1
            q.enqueue((e, nextCnt))
          }
        }
      }
    }
  }

  if (inDegree.exists(_ > 0) || ans.exists(_ == 0))
    println(-1)
  else
    println(ans.max)
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.deep))
  }
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
