n = gets.to_i
a = []
n.times do |i|
    a[i] = gets.split.map{|x| x.to_i - 1}
end

ans = 0
check = Array.new(n)
while true do
    count = 0
    c = false
    check.fill(true)

    n.times do |i|
        if (opp = a[i][0]) == nil then
            count += 1
            next
        end

        if check[i] && check[opp] && a[opp][0] == i then
            check[i] = false
            check[opp] = false
            a[i].delete_at(0)
            a[opp].delete_at(0)
            c = true
        end
    end

    break if count == n

    unless c then
        puts -1
        exit
    end 

    ans += 1
end

puts ans