#!/usr/bin/env python3

#ABC139 E



import sys

import math

import bisect

import time

sys.setrecursionlimit(1000000000)

from heapq import heappush, heappop,heappushpop

from collections import defaultdict

from itertools import accumulate

from collections import Counter

from collections import deque

from operator import itemgetter

from itertools import permutations

mod = 10**9 + 7

inf = float('inf')

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))



n = I()

a = [deque() for _ in range(n)]

for i in range(1,n+1):

    x = LI()

    for j in x:

        a[i-1].append((i,j))

ans = 0

s = time.time()

for j in range(n*(n-1)//2+1):

    check = [1]*n

    flg = False

    flg2 = False

    if time.time() - s > 1.6:

        print((n*(n-1)//2))

        quit()

    for i in range(n):

        if len(a[i]) == 0 or check[i] == 0:

            continue

        (x,y) = a[i].popleft()

        flg2 = True

        check[i] = 0

        if a[y-1][0] == (y,x) and check[y-1]:

            a[y-1].popleft()

            flg = True

            check[y-1] = 0

        else:

            a[i].appendleft((x,y))

    if flg:

        ans += 1

    if not flg:

        for i in a:

            if len(i) != 0:

                print((-1))

                quit()

    if not flg2:

        print(ans)

        quit()

#!/usr/bin/env python3

#ABC139 E



import sys

import math

import bisect

import time

sys.setrecursionlimit(1000000000)

from heapq import heappush, heappop,heappushpop

from collections import defaultdict

from itertools import accumulate

from collections import Counter

from collections import deque

from operator import itemgetter

from itertools import permutations

mod = 10**9 + 7

inf = float('inf')

def I(): return int(sys.stdin.readline())

def LI(): return list(map(int,sys.stdin.readline().split()))



n = I()

a = [LI() + [-1] for _ in range(n)]

cnt,day = [0]*n,[0]*n

que = deque([i for i in range(n)])

while que:

    u = que.popleft()

    v = a[u][cnt[u]] - 1

    if a[v][cnt[v]] - 1 == u:

        day[u] = max(day[u],day[v]) + 1

        day[v] = day[u]

        cnt[u] += 1

        cnt[v] += 1

        que.append(u)

        que.append(v)

for i in range(n):

    if cnt[i] != n-1:

        print((-1))

        quit()

print((max(day)))

#!/usr/bin/env python3
# ABC139 E
import sys
import math
import bisect
import time

sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations

mod = 10**9 + 7
inf = float("inf")


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


n = I()
a = [deque() for _ in range(n)]
for i in range(1, n + 1):
    x = LI()
    for j in x:
        a[i - 1].append((i, j))
ans = 0
s = time.time()
for j in range(n * (n - 1) // 2 + 1):
    check = [1] * n
    flg = False
    flg2 = False
    if time.time() - s > 1.6:
        print((n * (n - 1) // 2))
        quit()
    for i in range(n):
        if len(a[i]) == 0 or check[i] == 0:
            continue
        (x, y) = a[i].popleft()
        flg2 = True
        check[i] = 0
        if a[y - 1][0] == (y, x) and check[y - 1]:
            a[y - 1].popleft()
            flg = True
            check[y - 1] = 0
        else:
            a[i].appendleft((x, y))
    if flg:
        ans += 1
    if not flg:
        for i in a:
            if len(i) != 0:
                print((-1))
                quit()
    if not flg2:
        print(ans)
        quit()

#!/usr/bin/env python3
# ABC139 E
import sys
import math
import bisect
import time

sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations

mod = 10**9 + 7
inf = float("inf")


def I():
    return int(sys.stdin.readline())


def LI():
    return list(map(int, sys.stdin.readline().split()))


n = I()
a = [LI() + [-1] for _ in range(n)]
cnt, day = [0] * n, [0] * n
que = deque([i for i in range(n)])
while que:
    u = que.popleft()
    v = a[u][cnt[u]] - 1
    if a[v][cnt[v]] - 1 == u:
        day[u] = max(day[u], day[v]) + 1
        day[v] = day[u]
        cnt[u] += 1
        cnt[v] += 1
        que.append(u)
        que.append(v)
for i in range(n):
    if cnt[i] != n - 1:
        print((-1))
        quit()
print((max(day)))

-a = [deque() for _ in range(n)] -for i in range(1, n + 1): -    x = LI() -    for j in x: -        a[i - 1].append((i, j)) -ans = 0 -s = time.time() -for j in range(n * (n - 1) // 2 + 1): -    check = [1] * n -    flg = False -    flg2 = False -    if time.time() - s > 1.6: -        print((n * (n - 1) // 2)) +a = [LI() + [-1] for _ in range(n)] +cnt, day = [0] * n, [0] * n +que = deque([i for i in range(n)]) +while que: +    u = que.popleft() +    v = a[u][cnt[u]] - 1 +    if a[v][cnt[v]] - 1 == u: +        day[u] = max(day[u], day[v]) + 1 +        day[v] = day[u] +        cnt[u] += 1 +        cnt[v] += 1 +        que.append(u) +        que.append(v) +for i in range(n): +    if cnt[i] != n - 1: +        print((-1)) -    for i in range(n): -        if len(a[i]) == 0 or check[i] == 0: -            continue -        (x, y) = a[i].popleft() -        flg2 = True -        check[i] = 0 -        if a[y - 1][0] == (y, x) and check[y - 1]: -            a[y - 1].popleft() -            flg = True -            check[y - 1] = 0 -        else: -            a[i].appendleft((x, y)) -    if flg: -        ans += 1 -    if not flg: -        for i in a: -            if len(i) != 0: -                print((-1)) -                quit() -    if not flg2: -        print(ans) -        quit() +print((max(day)))