import time

s_time = time.time()





n = int(eval(input()))

a = [list(map(int, input().split())) for i in range(n)]



memo = {}

for i in range(n):

    memo[i] = 0



cnt = 0

while True:

    visited = [False] * n

    flag = False

    for i in range(n):

        if memo[i] == len(a[0]):

            continue

        if visited[i] or visited[a[i][memo[i]] - 1]:

            continue

        visited[i] = True

        if i + 1 == a[a[i][memo[i]] - 1][memo[a[i][memo[i]] - 1]]:

            visited[a[i][memo[i]] - 1] = True

            flag = True

            tmp = memo[i]

            memo[i] += 1

            memo[a[i][tmp] - 1] += 1

    if flag:

        cnt += 1

    else:

        for i in memo:

            if memo[i] != len(a[0]):

                print((-1))

                exit()

        break

    if abs(s_time - time.time()) > 1.7:

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

        exit()

print(cnt)

import sys

sys.setrecursionlimit(2000000)





n = int(eval(input()))

a = [list(map(int, input().split())) for i in range(n)]

for i in range(n):

    for j in range(len(a[0])):

        a[i][j] -= 1



visited = [[-1] *(len(a[0])) for i in range(n)]

memo = [[-1] * (n) for i in range(n)]



for i in range(n):

    for j in range(len(a[0])):

        memo[i][a[i][j]] = j



def solve(i_me, j_me, rec_times):

    if rec_times >= 10000:

        print((-1))

        exit()

    i_you = a[i_me][j_me]

    j_you = memo[i_you][i_me]

    

    if visited[i_me][j_me] > 0 and visited[i_you][j_you] > 0:

        return max(visited[i_me][j_me], visited[i_you][j_you])



    if j_me == 0 and j_you == 0:

        visited[i_me][j_me] = 1

        visited[i_you][j_you] = 1

        return 1

    

    if j_you == 0:

        day = solve(i_me, j_me - 1, rec_times + 1) + 1

    elif j_me == 0:

        day = solve(i_you, j_you - 1, rec_times + 1) + 1    

    else:

        day = max(solve(i_me, j_me - 1, rec_times + 1), solve(i_you, j_you - 1, rec_times + 1)) + 1

    visited[i_me][j_me] = day

    visited[i_you][j_you] = day

    return day

    

for i in range(n):

    solve(i, len(a[0]) - 1, 0)

ans = 0

for i in range(n):

    ans = max(ans,visited[i][-1])

print(ans)

import time

s_time = time.time()
n = int(eval(input()))
a = [list(map(int, input().split())) for i in range(n)]
memo = {}
for i in range(n):
    memo[i] = 0
cnt = 0
while True:
    visited = [False] * n
    flag = False
    for i in range(n):
        if memo[i] == len(a[0]):
            continue
        if visited[i] or visited[a[i][memo[i]] - 1]:
            continue
        visited[i] = True
        if i + 1 == a[a[i][memo[i]] - 1][memo[a[i][memo[i]] - 1]]:
            visited[a[i][memo[i]] - 1] = True
            flag = True
            tmp = memo[i]
            memo[i] += 1
            memo[a[i][tmp] - 1] += 1
    if flag:
        cnt += 1
    else:
        for i in memo:
            if memo[i] != len(a[0]):
                print((-1))
                exit()
        break
    if abs(s_time - time.time()) > 1.7:
        print((n * (n - 1) // 2))
        exit()
print(cnt)

import sys

sys.setrecursionlimit(2000000)
n = int(eval(input()))
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    for j in range(len(a[0])):
        a[i][j] -= 1
visited = [[-1] * (len(a[0])) for i in range(n)]
memo = [[-1] * (n) for i in range(n)]
for i in range(n):
    for j in range(len(a[0])):
        memo[i][a[i][j]] = j


def solve(i_me, j_me, rec_times):
    if rec_times >= 10000:
        print((-1))
        exit()
    i_you = a[i_me][j_me]
    j_you = memo[i_you][i_me]
    if visited[i_me][j_me] > 0 and visited[i_you][j_you] > 0:
        return max(visited[i_me][j_me], visited[i_you][j_you])
    if j_me == 0 and j_you == 0:
        visited[i_me][j_me] = 1
        visited[i_you][j_you] = 1
        return 1
    if j_you == 0:
        day = solve(i_me, j_me - 1, rec_times + 1) + 1
    elif j_me == 0:
        day = solve(i_you, j_you - 1, rec_times + 1) + 1
    else:
        day = (
            max(
                solve(i_me, j_me - 1, rec_times + 1),
                solve(i_you, j_you - 1, rec_times + 1),
            )
            + 1
        )
    visited[i_me][j_me] = day
    visited[i_you][j_you] = day
    return day


for i in range(n):
    solve(i, len(a[0]) - 1, 0)
ans = 0
for i in range(n):
    ans = max(ans, visited[i][-1])
print(ans)

-import time +import sys -s_time = time.time() +sys.setrecursionlimit(2000000) -memo = {} -    memo[i] = 0 -cnt = 0 -while True: -    visited = [False] * n -    flag = False -    for i in range(n): -        if memo[i] == len(a[0]): -            continue -        if visited[i] or visited[a[i][memo[i]] - 1]: -            continue -        visited[i] = True -        if i + 1 == a[a[i][memo[i]] - 1][memo[a[i][memo[i]] - 1]]: -            visited[a[i][memo[i]] - 1] = True -            flag = True -            tmp = memo[i] -            memo[i] += 1 -            memo[a[i][tmp] - 1] += 1 -    if flag: -        cnt += 1 +    for j in range(len(a[0])): +        a[i][j] -= 1 +visited = [[-1] * (len(a[0])) for i in range(n)] +memo = [[-1] * (n) for i in range(n)] +for i in range(n): +    for j in range(len(a[0])): +        memo[i][a[i][j]] = j + + +def solve(i_me, j_me, rec_times): +    if rec_times >= 10000: +        print((-1)) +        exit() +    i_you = a[i_me][j_me] +    j_you = memo[i_you][i_me] +    if visited[i_me][j_me] > 0 and visited[i_you][j_you] > 0: +        return max(visited[i_me][j_me], visited[i_you][j_you]) +    if j_me == 0 and j_you == 0: +        visited[i_me][j_me] = 1 +        visited[i_you][j_you] = 1 +        return 1 +    if j_you == 0: +        day = solve(i_me, j_me - 1, rec_times + 1) + 1 +    elif j_me == 0: +        day = solve(i_you, j_you - 1, rec_times + 1) + 1 -        for i in memo: -            if memo[i] != len(a[0]): -                print((-1)) -                exit() -        break -    if abs(s_time - time.time()) > 1.7: -        print((n * (n - 1) // 2)) -        exit() -print(cnt) +        day = ( +            max( +                solve(i_me, j_me - 1, rec_times + 1), +                solve(i_you, j_you - 1, rec_times + 1), +            ) +            + 1 +        ) +    visited[i_me][j_me] = day +    visited[i_you][j_you] = day +    return day + + +for i in range(n): +    solve(i, len(a[0]) - 1, 0) +ans = 0 +for i in range(n): +    ans = max(ans, visited[i][-1]) +print(ans)