import sys

import collections

import copy



input = sys.stdin.readline





def main():

    N = int(eval(input()))

    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]



    day = 0

    c = 0

    nxt = [x for x in range(N)]

    oppo = [0] * N



    while c != N * (N - 1):

        day += 1



        for i in nxt:

            oppo[i] = A[i].popleft() if A[i] else -1



        tmp = set()

        for i in nxt:

            if i == -1 or i in tmp:

                continue

            if i == oppo[oppo[i] - 1] - 1:

                tmp.add(i)

                tmp.add(oppo[i] - 1)

                c += 2

        if not tmp:

            print((-1))

            return



        nxt = tmp



    print(day)





if __name__ == '__main__':

    main()

import sys

import collections

from collections import defaultdict



sys.setrecursionlimit(10 ** 8)



input = sys.stdin.readline





def main():

    N = int(eval(input()))

    A = [[int(x) for x in input().split()] for _ in range(N)]



    q = [collections.deque() for j in range(N)]



    for i, a in enumerate(A):

        for aa in a:

            q[i].append(aa)



    ans = 0



    current = defaultdict(int)

    for i, v in enumerate(q):

        current[i + 1] = v[0]



    while True:

        v = set()

        for k in list(current.keys()):

            if k == q[current[k] - 1][0]:

                v.add(k)

                v.add(current[k])



        current = defaultdict(int)



        if len(v) == 0:

            break



        for vv in v:

            q[vv - 1].popleft()

            if len(q[vv - 1]):

                current[vv] = q[vv - 1][0]



        ans += 1



    # 全て空なら

    size = 0

    for i in range(N):

        size += len(q[i])



    if size == 0:

        print(ans)

    else:

        print((-1))





if __name__ == '__main__':

    main()

import sys
import collections
import copy

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]
    day = 0
    c = 0
    nxt = [x for x in range(N)]
    oppo = [0] * N
    while c != N * (N - 1):
        day += 1
        for i in nxt:
            oppo[i] = A[i].popleft() if A[i] else -1
        tmp = set()
        for i in nxt:
            if i == -1 or i in tmp:
                continue
            if i == oppo[oppo[i] - 1] - 1:
                tmp.add(i)
                tmp.add(oppo[i] - 1)
                c += 2
        if not tmp:
            print((-1))
            return
        nxt = tmp
    print(day)


if __name__ == "__main__":
    main()

import sys
import collections
from collections import defaultdict

sys.setrecursionlimit(10**8)
input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [[int(x) for x in input().split()] for _ in range(N)]
    q = [collections.deque() for j in range(N)]
    for i, a in enumerate(A):
        for aa in a:
            q[i].append(aa)
    ans = 0
    current = defaultdict(int)
    for i, v in enumerate(q):
        current[i + 1] = v[0]
    while True:
        v = set()
        for k in list(current.keys()):
            if k == q[current[k] - 1][0]:
                v.add(k)
                v.add(current[k])
        current = defaultdict(int)
        if len(v) == 0:
            break
        for vv in v:
            q[vv - 1].popleft()
            if len(q[vv - 1]):
                current[vv] = q[vv - 1][0]
        ans += 1
    # 全て空なら
    size = 0
    for i in range(N):
        size += len(q[i])
    if size == 0:
        print(ans)
    else:
        print((-1))


if __name__ == "__main__":
    main()

-import copy +from collections import defaultdict +sys.setrecursionlimit(10**8) -    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)] -    day = 0 -    c = 0 -    nxt = [x for x in range(N)] -    oppo = [0] * N -    while c != N * (N - 1): -        day += 1 -        for i in nxt: -            oppo[i] = A[i].popleft() if A[i] else -1 -        tmp = set() -        for i in nxt: -            if i == -1 or i in tmp: -                continue -            if i == oppo[oppo[i] - 1] - 1: -                tmp.add(i) -                tmp.add(oppo[i] - 1) -                c += 2 -        if not tmp: -            print((-1)) -            return -        nxt = tmp -    print(day) +    A = [[int(x) for x in input().split()] for _ in range(N)] +    q = [collections.deque() for j in range(N)] +    for i, a in enumerate(A): +        for aa in a: +            q[i].append(aa) +    ans = 0 +    current = defaultdict(int) +    for i, v in enumerate(q): +        current[i + 1] = v[0] +    while True: +        v = set() +        for k in list(current.keys()): +            if k == q[current[k] - 1][0]: +                v.add(k) +                v.add(current[k]) +        current = defaultdict(int) +        if len(v) == 0: +            break +        for vv in v: +            q[vv - 1].popleft() +            if len(q[vv - 1]): +                current[vv] = q[vv - 1][0] +        ans += 1 +    # 全て空なら +    size = 0 +    for i in range(N): +        size += len(q[i]) +    if size == 0: +        print(ans) +    else: +        print((-1))