from collections import deque



N = int(eval(input()))

A = [list(map(int, input().split())) for _ in range(N)]



graph = [[] for _ in range(N * N)]

ins = [0] * (N * N)



for i, nodes in enumerate(A):

    for j1, j2 in zip(nodes[:-1], nodes[1:]):

        n1 = max(i, j1 - 1) * N + min(i, j1 - 1)

        n2 = max(i, j2 - 1) * N + min(i, j2 - 1)

        graph[n1].append(n2)

        ins[n2] += 1



visited = [False] * (N * N)

for i in range(N):

    visited[N * i + i] = True



que = deque()

for i, v in enumerate(ins):

    if v == 0:

        que.append((i, 1))



dist = [0] * (N * N)

while que:

    cur, d = que.popleft()

    

    visited[cur] = True

    dist[cur] = d

    for v in graph[cur]:

        if visited[v]:

            continue

            

        ins[v] -= 1

        if ins[v] == 0:

            que.append((v, d + 1))



print((max(dist) if all(visited) else -1))

from collections import deque



N = int(eval(input()))

X = [list(map(int, input().split())) for _ in range(N)]



graph = [[] for _ in range(N * N)]

ins = [0] * (N * N)



for i, nodes in enumerate(X):

    for j1, j2 in zip(nodes[:-1], nodes[1:]):

        n1 = max(i, j1 - 1) * N + min(i, j1 - 1)

        n2 = max(i, j2 - 1) * N + min(i, j2 - 1)

        graph[n1].append(n2)

        ins[n2] += 1



dist = [0] * (N * N)

visited = [False] * (N * N)

q = deque()

for i in range(N):

    visited[N * i + i] = True



for i, v in enumerate(ins):

    if v == 0:

        q.append((i, 1))



while q:

    u, d = q.popleft()

    visited[u] = True

    dist[u] = d



    for v in graph[u]:

        if visited[v]:

            continue

            

        ins[v] -= 1

        if ins[v] == 0:

            q.append((v, d + 1))

            

print((max(dist) if all(visited) else -1))



from collections import deque

N = int(eval(input()))
A = [list(map(int, input().split())) for _ in range(N)]
graph = [[] for _ in range(N * N)]
ins = [0] * (N * N)
for i, nodes in enumerate(A):
    for j1, j2 in zip(nodes[:-1], nodes[1:]):
        n1 = max(i, j1 - 1) * N + min(i, j1 - 1)
        n2 = max(i, j2 - 1) * N + min(i, j2 - 1)
        graph[n1].append(n2)
        ins[n2] += 1
visited = [False] * (N * N)
for i in range(N):
    visited[N * i + i] = True
que = deque()
for i, v in enumerate(ins):
    if v == 0:
        que.append((i, 1))
dist = [0] * (N * N)
while que:
    cur, d = que.popleft()
    visited[cur] = True
    dist[cur] = d
    for v in graph[cur]:
        if visited[v]:
            continue
        ins[v] -= 1
        if ins[v] == 0:
            que.append((v, d + 1))
print((max(dist) if all(visited) else -1))

from collections import deque

N = int(eval(input()))
X = [list(map(int, input().split())) for _ in range(N)]
graph = [[] for _ in range(N * N)]
ins = [0] * (N * N)
for i, nodes in enumerate(X):
    for j1, j2 in zip(nodes[:-1], nodes[1:]):
        n1 = max(i, j1 - 1) * N + min(i, j1 - 1)
        n2 = max(i, j2 - 1) * N + min(i, j2 - 1)
        graph[n1].append(n2)
        ins[n2] += 1
dist = [0] * (N * N)
visited = [False] * (N * N)
q = deque()
for i in range(N):
    visited[N * i + i] = True
for i, v in enumerate(ins):
    if v == 0:
        q.append((i, 1))
while q:
    u, d = q.popleft()
    visited[u] = True
    dist[u] = d
    for v in graph[u]:
        if visited[v]:
            continue
        ins[v] -= 1
        if ins[v] == 0:
            q.append((v, d + 1))
print((max(dist) if all(visited) else -1))

-A = [list(map(int, input().split())) for _ in range(N)] +X = [list(map(int, input().split())) for _ in range(N)] -for i, nodes in enumerate(A): +for i, nodes in enumerate(X): +dist = [0] * (N * N) +q = deque() -que = deque() -        que.append((i, 1)) -dist = [0] * (N * N) -while que: -    cur, d = que.popleft() -    visited[cur] = True -    dist[cur] = d -    for v in graph[cur]: +        q.append((i, 1)) +while q: +    u, d = q.popleft() +    visited[u] = True +    dist[u] = d +    for v in graph[u]: -            que.append((v, d + 1)) +            q.append((v, d + 1))