Problem:
 Score : 500 points    Problem Statement N players will participate in a tennis tournament. We will call them Player 1 , Player 2 , \ldots , Player N .  The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.   Each player plays at most one matches in a day.  Each player i  (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.       Constraints  3 \leq N \leq 1000  1 \leq A_{i, j} \leq N  A_{i, j} \neq i  A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.         Input Input is given from Standard Input in the following format:  N  A_{1, 1}  A_{1, 2}  \ldots  A_{1, N-1}  A_{2, 1}  A_{2, 2}  \ldots  A_{2, N-1}  :  A_{N, 1}  A_{N, 2}  \ldots  A_{N, N-1}       Output If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1 .        Sample Input 1 3
2 3
1 3
1 2      Sample Output 1 3  All the conditions can be satisfied if the matches are scheduled for three days as follows:   Day 1 : Player 1 vs Player 2  Day 2 : Player 1 vs Player 3  Day 3 : Player 2 vs Player 3   This is the minimum number of days required.       Sample Input 2 4
2 3 4
1 3 4
4 1 2
3 1 2      Sample Output 2 4  All the conditions can be satisfied if the matches are scheduled for four days as follows:   Day 1 : Player 1 vs Player 2 , Player 3 vs Player 4  Day 2 : Player 1 vs Player 3  Day 3 : Player 1 vs Player 4 , Player 2 vs Player 3  Day 4 : Player 2 vs Player 4   This is the minimum number of days required.       Sample Input 3 3
2 3
3 1
1 2      Sample Output 3 -1  Any scheduling of the matches violates some condition.

Solution (Python):
import subprocess
import gzip, base64, os
import stat

gz = b'H4sIAJ3vbF0C/+19e3xUxfX43c1reWWD9RERZdEooBCyJECCCglh4S5sAEnAFB+XzWaXrO6ru3dDoqDYBOu6RPlaa20/bb/82m+t7ddWf7W1+IJEHsE3viiKIlrUXSMaXxBF2N85M3M3c+/ujdD29/3+4yqZPeeeM3POmTNnzsy9O/d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(pymain, "wb") as f:
    f.write(bin)
os.chmod(pymain, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
if p == "":
    pymain = "./" + pymain
subprocess.call(pymain)



'''

import numpy as np
MAX_VAlUE = 1000000


def dfs(x, y, edges, deps, mark):
    if x > y:
        x, y = y, x
    if deps[x, y] > -1:
        return deps[x, y]

    e1 = edges[x, y]
    if e1 > -1:
        if mark[x, e1] == 0:
            mark[x, e1] = 1
            d1 = dfs(x, e1, edges, deps, mark)
        else:
            return MAX_VAlUE  
    else:
        d1 = 0

    e2 = edges[y, x]
    if e2 > -1:
        if mark[y, e2] == 0:
            mark[y, e2] = 1
            d2 = dfs(y, e2, edges, deps, mark)
        else:
            return MAX_VAlUE  
    else:
        d2 = 0

    deps[x, y] = 1 + max(d1, d2)
    return deps[x, y]

def solve(A, edges):
    N = A.shape[0]
    mark = np.zeros((N, N), dtype=np.int64)
    deps = np.empty((N, N), dtype=np.int64)
    full(deps, -1)
    ans = -1
    a = A[:, 0].flatten()
    for i in range(N):
        if a[i] > i and a[a[i]] == i:
            ans = max(ans, dfs(i, a[i], edges, deps, mark))
    if ans >= MAX_VAlUE:
        return -1
    return ans


def set_edges(A, edges):
    for i in range(A.shape[0]):
        a = A[i]
        e = edges[i]
        for j in range(A.shape[1] - 1):
            e[a[j]] = a[j+1]

def full(a, x):
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            a[i, j] = x

def main2():
    stdin = open('/dev/stdin')
    N = int(stdin.readline())
    A = np.empty((N, N - 1), dtype=np.int64)
    for i in range(N):
        j = 0
        for c in stdin.readline().split():
            A[i, j] = int(c) - 1
            j += 1
    edges = np.empty((N, N), dtype=np.int64)
    full(edges, -1)
    set_edges(A, edges)
    ans = solve(A, edges)
    print(ans)

'''

Solution (Python):
from collections import deque

N = int(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)