fun main() {
    val n = readLine()!!.toInt()
    val m = n - 1
    val t = Array(n) { IntArray(m) }
    for (i in 0 until n) {
        val a = readLine()!!.split(' ').map { it.toInt() - 1 }
        for (j in 0 until m) {
            t[i][j] = a[j]
        }
    }

    val cur = IntArray(n)
    var q = mutableSetOf<Pair<Int, Int>>()

    fun check(i: Int) {
        if (cur[i] == m) return
        val j = t[i][cur[i]]
        if (cur[j] == m) return
        if (t[j][cur[j]] != i) return
        q.add(if (i < j) i to j else j to i)
    }

    for (i in 0 until n) {
        check(i)
    }

    var day = 0

    while (q.isNotEmpty()) {
        day++
        val prev = q
        q = mutableSetOf()

        for ((i, j) in prev) {
            cur[i]++
            cur[j]++
        }

        for ((i, j) in prev) {
            check(i)
            check(j)
        }
    }

    val ans = if (cur.all { it == m }) {
        day
    } else {
        -1
    }

    println(ans)
}
