import sys

from collections import deque



n = int(eval(input()))

matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]

q = deque(list(range(n)))

depth = [0] * n

waiting = [-1] * n



while q:

    a = q.popleft()

    ma = matches[a]

    if not ma:

        continue

    b = ma.pop()

    if waiting[b] == a:

        depth[a] = depth[b] = max(depth[a], depth[b]) + 1

        q.append(a)

        q.append(b)

    else:

        waiting[a] = b



if any(matches):

    print((-1))

else:

    print((max(depth)))

import sys

from collections import deque



n = int(eval(input()))

matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]

q = deque(list(range(n)))

depth = [0] * n

waiting = [-1] * n



while q:

    a = q.popleft()

    b = matches[a].pop()

    if waiting[b] == a:

        depth[a] = depth[b] = max(depth[a], depth[b]) + 1

        if matches[a]:

            q.append(a)

        if matches[b]:

            q.append(b)

    else:

        waiting[a] = b



if any(matches):

    print((-1))

else:

    print((max(depth)))

import sys
from collections import deque

n = int(eval(input()))
matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]
q = deque(list(range(n)))
depth = [0] * n
waiting = [-1] * n
while q:
    a = q.popleft()
    ma = matches[a]
    if not ma:
        continue
    b = ma.pop()
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a], depth[b]) + 1
        q.append(a)
        q.append(b)
    else:
        waiting[a] = b
if any(matches):
    print((-1))
else:
    print((max(depth)))

import sys
from collections import deque

n = int(eval(input()))
matches = [[a - 1 for a in map(int, line.split())] for line in sys.stdin]
q = deque(list(range(n)))
depth = [0] * n
waiting = [-1] * n
while q:
    a = q.popleft()
    b = matches[a].pop()
    if waiting[b] == a:
        depth[a] = depth[b] = max(depth[a], depth[b]) + 1
        if matches[a]:
            q.append(a)
        if matches[b]:
            q.append(b)
    else:
        waiting[a] = b
if any(matches):
    print((-1))
else:
    print((max(depth)))

-    ma = matches[a] -    if not ma: -        continue -    b = ma.pop() +    b = matches[a].pop() -        q.append(a) -        q.append(b) +        if matches[a]: +            q.append(a) +        if matches[b]: +            q.append(b)