Module Module1

sub Main(ByVal args() as String)
    dim N as integer = Console.ReadLine()
    dim a(N,N-1) as integer
    dim e(N) as integer
    dim b(N) as boolean
    for i as integer = 0 to N-1
        dim arr() as string = Split(Console.ReadLine())
        for j as integer = 0 to N-2
            a(i, j) = arr(j)-1
        next j
        e(i) = 0
        b(i) = true
    next i

    dim d as integer = 0
    dim c as boolean = false
    dim ba as boolean = true
    do
        ba = false
        for i as integer = 0 to N
            if (not b(i)) andalso e(i) < N-1 then
                b(i) = true
                c = false
            end if
        next i
        if c then
            exit do
        end if

        for i as integer = 0 to N-2
            if not b(i) then
                continue for
            end if
            for j as integer = i+1 to N-1
                if b(j) andalso j=a(i, e(i)) andalso i=a(j, e(j)) then
                    b(i) = false
                    b(j) = false
                    e(i) += 1
                    e(j) += 1
                    ba = true
                    exit for
                end if
            next j
        next i

        if ba then
            d += 1
            c = true
        else
            exit do
        end if
    loop

    if not ba andalso not c then
        Console.WriteLine("-1")
    else
        Console.WriteLine(d)
    end if
end sub

end Module