import sys



input = sys.stdin.readline

sys.setrecursionlimit(10**6)



n = int(eval(input()))

graph = [list() for _ in range(n**2)]



id_index = [[i+j*n for i in range(n)] for j in range(n)]

index_list = []



def toId(p, o):

    if p > o:

        index = id_index[o][p]

        index_list.append(index)

        return index

    else: return id_index[p][o]



v_len = [-1 for _ in range(n)] * n

finished = [False for _ in range(n)] * n



def dfs(id):

    next_list = graph[id]



    if v_len[id] != -1:

        if not finished[id]:

            print((-1))

            sys.exit()

        return v_len[id]



    v_len[id] = 1



    for next in next_list:

        v_len[id] = max(v_len[id], dfs(next)+1)



    finished[id] = True

    return v_len[id]



for p in range(n):

    a = [toId(p, int(x)-1) for x in input().split()]

    for i in range(n-2):

        graph[a[i]].append(a[i+1])

    

ans = 0



for id in index_list:

    ans = max(dfs(id), ans)



print(ans)
import sys

from collections import deque

input = lambda: sys.stdin.readline().rstrip()

tdlist = lambda i, j, value=0: [[value] * j for _ in range(i)]



n = int(eval(input()))

m = n * (n-1) // 2

graph = [list() for _ in range(m)]

deg = [0] * m



to_id = tdlist(n, n)

cnt = 0

for i in range(n):

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

        to_id[i][j], to_id[j][i] = cnt, cnt

        cnt += 1



for i in range(n):

    a = list(map(int, input().split()))

    for j in range(n-2):

        v = to_id[i][a[j]-1]

        u = to_id[i][a[j+1]-1]

        graph[v].append(u)

        deg[u] += 1



q = deque(v for v in range(m) if deg[v] == 0)

cnt = 0

dp = [0] * m



while q:

    v = q.popleft()

    cnt += 1

    for u in graph[v]:

        deg[u] -= 1

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

        if deg[u] == 0:

            q.append(u)



print((-1 if cnt != m else max(dp)+1))

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n = int(eval(input()))
graph = [list() for _ in range(n**2)]
id_index = [[i + j * n for i in range(n)] for j in range(n)]
index_list = []


def toId(p, o):
    if p > o:
        index = id_index[o][p]
        index_list.append(index)
        return index
    else:
        return id_index[p][o]


v_len = [-1 for _ in range(n)] * n
finished = [False for _ in range(n)] * n


def dfs(id):
    next_list = graph[id]
    if v_len[id] != -1:
        if not finished[id]:
            print((-1))
            sys.exit()
        return v_len[id]
    v_len[id] = 1
    for next in next_list:
        v_len[id] = max(v_len[id], dfs(next) + 1)
    finished[id] = True
    return v_len[id]


for p in range(n):
    a = [toId(p, int(x) - 1) for x in input().split()]
    for i in range(n - 2):
        graph[a[i]].append(a[i + 1])
ans = 0
for id in index_list:
    ans = max(dfs(id), ans)
print(ans)

import sys
from collections import deque

input = lambda: sys.stdin.readline().rstrip()
tdlist = lambda i, j, value=0: [[value] * j for _ in range(i)]
n = int(eval(input()))
m = n * (n - 1) // 2
graph = [list() for _ in range(m)]
deg = [0] * m
to_id = tdlist(n, n)
cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        to_id[i][j], to_id[j][i] = cnt, cnt
        cnt += 1
for i in range(n):
    a = list(map(int, input().split()))
    for j in range(n - 2):
        v = to_id[i][a[j] - 1]
        u = to_id[i][a[j + 1] - 1]
        graph[v].append(u)
        deg[u] += 1
q = deque(v for v in range(m) if deg[v] == 0)
cnt = 0
dp = [0] * m
while q:
    v = q.popleft()
    cnt += 1
    for u in graph[v]:
        deg[u] -= 1
        dp[u] = max(dp[u], dp[v] + 1)
        if deg[u] == 0:
            q.append(u)
print((-1 if cnt != m else max(dp) + 1))

+from collections import deque -input = sys.stdin.readline -sys.setrecursionlimit(10**6) +input = lambda: sys.stdin.readline().rstrip() +tdlist = lambda i, j, value=0: [[value] * j for _ in range(i)] -graph = [list() for _ in range(n**2)] -id_index = [[i + j * n for i in range(n)] for j in range(n)] -index_list = [] - - -def toId(p, o): -    if p > o: -        index = id_index[o][p] -        index_list.append(index) -        return index -    else: -        return id_index[p][o] - - -v_len = [-1 for _ in range(n)] * n -finished = [False for _ in range(n)] * n - - -def dfs(id): -    next_list = graph[id] -    if v_len[id] != -1: -        if not finished[id]: -            print((-1)) -            sys.exit() -        return v_len[id] -    v_len[id] = 1 -    for next in next_list: -        v_len[id] = max(v_len[id], dfs(next) + 1) -    finished[id] = True -    return v_len[id] - - -for p in range(n): -    a = [toId(p, int(x) - 1) for x in input().split()] -    for i in range(n - 2): -        graph[a[i]].append(a[i + 1]) -ans = 0 -for id in index_list: -    ans = max(dfs(id), ans) -print(ans) +m = n * (n - 1) // 2 +graph = [list() for _ in range(m)] +deg = [0] * m +to_id = tdlist(n, n) +cnt = 0 +for i in range(n): +    for j in range(i + 1, n): +        to_id[i][j], to_id[j][i] = cnt, cnt +        cnt += 1 +for i in range(n): +    a = list(map(int, input().split())) +    for j in range(n - 2): +        v = to_id[i][a[j] - 1] +        u = to_id[i][a[j + 1] - 1] +        graph[v].append(u) +        deg[u] += 1 +q = deque(v for v in range(m) if deg[v] == 0) +cnt = 0 +dp = [0] * m +while q: +    v = q.popleft() +    cnt += 1 +    for u in graph[v]: +        deg[u] -= 1 +        dp[u] = max(dp[u], dp[v] + 1) +        if deg[u] == 0: +            q.append(u) +print((-1 if cnt != m else max(dp) + 1))