fun main(args: Array<String>) {
    val N = readInputLine().toInt()
    val A = Array(N, { readInputLine().split(" ").map { it.toInt() - 1 }})
    val AIndex = IntArray(N)
    val days = Array(N, {IntArray(N - 1)})

    while (true) {
        val used = mutableSetOf<Int>()
        var changed = false
        var allClear = true
        
        for (i in 0 until N) {
            for (j in AIndex[i] until N - 1) {
                allClear = false
                val other = A[i][AIndex[i]]
                
                if (A[other][AIndex[other]] == i) {
                    val newDay =
                        if (AIndex[other] == 0 && AIndex[i] == 0) {
                            1
                        } else if (AIndex[other] == 0) {
                            days[i][AIndex[i] - 1] + 1
                        } else if (AIndex[i] == 0) {
                            days[other][AIndex[other] - 1] + 1
                        } else {
                            Math.max(days[i][AIndex[i] - 1], days[other][AIndex[other] - 1]) + 1
                        }
                    days[i][AIndex[i]] = newDay
                    days[other][AIndex[other]] = newDay
                    
                    AIndex[i]++
                    AIndex[other]++
                    
                    changed = true
                } else {
                    break
                }
            }
        }
        
        if (allClear) {
            break
        }
        
        if (!changed) {
            println(-1)
            return
        }
    }
    
    var ans = 0
    
    for (i in 0 until N) {
        ans = Math.max(ans, days[i][N - 2])
    }
    
    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
