import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

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

def resolve():

    from itertools import product

    n=int(eval(input()))

    match=[[0]*n for _ in range(n)]

    V=0 # V=n(n-1)/2

    for i,j in product(list(range(n)),repeat=2):

        if(i>=j): continue

        match[i][j]=V

        V+=1



    def toID(i,j):

        if(i>j): i,j=j,i

        return match[i][j]



    E=[[] for _ in range(V)]

    for i in range(n):

        A=list([int(x)-1 for x in input().split()])

        for j in range(1,n-1):

            E[toID(i,A[j-1])].append(toID(i,A[j]))

    # 頂点数Vの有向グラフをdfsしてLongest pathの長さを求める

    # -1: unchecked, -2: checked and uncalculated

    dp=[-1]*V

    def dfs(v):

        if(dp[v]>=0): return

        dp[v]=-2

        length=0

        for nv in E[v]:

            if(dp[nv]==-2):

                print((-1))

                exit()

            if(dp[nv]==-1):

                dfs(nv)

            length=max(length,dp[nv]+1)

        dp[v]=length

    # calculate

    for i in range(V): dfs(i)

    print((max(dp)+1))

resolve()
import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

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

def resolve():

    n=int(eval(input()))

    # popするため逆順にしておく

    A=[list([int(x)-1 for x in input().split()]) for _ in range(n)]

    for i in range(n): A[i].reverse()

    stack=[]



    def check(i):

        if(not A[i]): return

        j=A[i][-1]

        if(A[j][-1]==i):

            stack.append((min(i,j),max(i,j)))



    # calculate

    for i in range(n): check(i)

    day=0

    while(stack):

        day+=1

        stack=list(set(stack)) # uniqueを取る

        prev,stack=stack,[]

        for i,j in prev:

            A[i].pop(); A[j].pop()

        for i,j in prev:

            check(i); check(j)

    print((-1 if(any(A[i] for i in range(n))) else day))

resolve()
import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    from itertools import product

    n = int(eval(input()))
    match = [[0] * n for _ in range(n)]
    V = 0  # V=n(n-1)/2
    for i, j in product(list(range(n)), repeat=2):
        if i >= j:
            continue
        match[i][j] = V
        V += 1

    def toID(i, j):
        if i > j:
            i, j = j, i
        return match[i][j]

    E = [[] for _ in range(V)]
    for i in range(n):
        A = list([int(x) - 1 for x in input().split()])
        for j in range(1, n - 1):
            E[toID(i, A[j - 1])].append(toID(i, A[j]))
    # 頂点数Vの有向グラフをdfsしてLongest pathの長さを求める
    # -1: unchecked, -2: checked and uncalculated
    dp = [-1] * V

    def dfs(v):
        if dp[v] >= 0:
            return
        dp[v] = -2
        length = 0
        for nv in E[v]:
            if dp[nv] == -2:
                print((-1))
                exit()
            if dp[nv] == -1:
                dfs(nv)
            length = max(length, dp[nv] + 1)
        dp[v] = length

    # calculate
    for i in range(V):
        dfs(i)
    print((max(dp) + 1))


resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()


def resolve():
    n = int(eval(input()))
    # popするため逆順にしておく
    A = [list([int(x) - 1 for x in input().split()]) for _ in range(n)]
    for i in range(n):
        A[i].reverse()
    stack = []

    def check(i):
        if not A[i]:
            return
        j = A[i][-1]
        if A[j][-1] == i:
            stack.append((min(i, j), max(i, j)))

    # calculate
    for i in range(n):
        check(i)
    day = 0
    while stack:
        day += 1
        stack = list(set(stack))  # uniqueを取る
        prev, stack = stack, []
        for i, j in prev:
            A[i].pop()
            A[j].pop()
        for i, j in prev:
            check(i)
            check(j)
    print((-1 if (any(A[i] for i in range(n))) else day))


resolve()

-    from itertools import product +    n = int(eval(input())) +    # popするため逆順にしておく +    A = [list([int(x) - 1 for x in input().split()]) for _ in range(n)] +    for i in range(n): +        A[i].reverse() +    stack = [] -    n = int(eval(input())) -    match = [[0] * n for _ in range(n)] -    V = 0  # V=n(n-1)/2 -    for i, j in product(list(range(n)), repeat=2): -        if i >= j: -            continue -        match[i][j] = V -        V += 1 - -    def toID(i, j): -        if i > j: -            i, j = j, i -        return match[i][j] - -    E = [[] for _ in range(V)] -    for i in range(n): -        A = list([int(x) - 1 for x in input().split()]) -        for j in range(1, n - 1): -            E[toID(i, A[j - 1])].append(toID(i, A[j])) -    # 頂点数Vの有向グラフをdfsしてLongest pathの長さを求める -    # -1: unchecked, -2: checked and uncalculated -    dp = [-1] * V - -    def dfs(v): -        if dp[v] >= 0: +    def check(i): +        if not A[i]: -        dp[v] = -2 -        length = 0 -        for nv in E[v]: -            if dp[nv] == -2: -                print((-1)) -                exit() -            if dp[nv] == -1: -                dfs(nv) -            length = max(length, dp[nv] + 1) -        dp[v] = length +        j = A[i][-1] +        if A[j][-1] == i: +            stack.append((min(i, j), max(i, j))) -    for i in range(V): -        dfs(i) -    print((max(dp) + 1)) +    for i in range(n): +        check(i) +    day = 0 +    while stack: +        day += 1 +        stack = list(set(stack))  # uniqueを取る +        prev, stack = stack, [] +        for i, j in prev: +            A[i].pop() +            A[j].pop() +        for i, j in prev: +            check(i) +            check(j) +    print((-1 if (any(A[i] for i in range(n))) else day))