import sys

 

read = sys.stdin.buffer.read

input = sys.stdin.buffer.readline

inputs = sys.stdin.buffer.readlines



def main():



  n=int(eval(input()))



  A=[list(map(int,input().split()))[::-1] for i in range(n)]



  day=0

  zan=[n-1]*n

  zann=sum(zan)

  sumi=set(range(n))



  while True:

    day+=1



    kouho=list(sumi)

    sumi=set()

  

    for i in kouho:

      try:

        if A[A[i][-1]-1][-1]-1==i:

          sumi.add(i)

          sumi.add(A[i][-1]-1)

      except:

        pass



    for k in sumi:

      A[k].pop()

      zan[k]-=1



    if zann==sum(zan):

      print((-1))

      exit(0)

  

    zann=sum(zan)

  

    if zann==0:

      print(day)

      exit(0)

    

if __name__ == "__main__":

	main()
import sys

input = sys.stdin.readline

from collections import deque

def main():

  N=int(eval(input()))

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

  Days=[0]*(N+1)

  NOW=[0]*(N+1)

  

  q=deque([i+1 for i in range(N)])

  

  while q:

    i=q.popleft()



    if not A[i]:

      continue

    a=A[i].popleft()

    

    if NOW[a]==i:

      Days[a]=Days[i]=max(Days[a],Days[i])+1

      q.append(i)

      q.append(a)

      NOW[a]=NOW[i]=0

    else:

      NOW[i]=a

    

  for i in range(1,N+1):

    if A[i]:

      print((-1))

      return

  print((max(Days)))

      



if __name__ == '__main__':

  main()
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


def main():
    n = int(eval(input()))
    A = [list(map(int, input().split()))[::-1] for i in range(n)]
    day = 0
    zan = [n - 1] * n
    zann = sum(zan)
    sumi = set(range(n))
    while True:
        day += 1
        kouho = list(sumi)
        sumi = set()
        for i in kouho:
            try:
                if A[A[i][-1] - 1][-1] - 1 == i:
                    sumi.add(i)
                    sumi.add(A[i][-1] - 1)
            except:
                pass
        for k in sumi:
            A[k].pop()
            zan[k] -= 1
        if zann == sum(zan):
            print((-1))
            exit(0)
        zann = sum(zan)
        if zann == 0:
            print(day)
            exit(0)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline
from collections import deque


def main():
    N = int(eval(input()))
    A = [deque()] + [deque(list(map(int, input().split()))) for _ in range(N)]
    Days = [0] * (N + 1)
    NOW = [0] * (N + 1)
    q = deque([i + 1 for i in range(N)])
    while q:
        i = q.popleft()
        if not A[i]:
            continue
        a = A[i].popleft()
        if NOW[a] == i:
            Days[a] = Days[i] = max(Days[a], Days[i]) + 1
            q.append(i)
            q.append(a)
            NOW[a] = NOW[i] = 0
        else:
            NOW[i] = a
    for i in range(1, N + 1):
        if A[i]:
            print((-1))
            return
    print((max(Days)))


if __name__ == "__main__":
    main()

-read = sys.stdin.buffer.read -input = sys.stdin.buffer.readline -inputs = sys.stdin.buffer.readlines +input = sys.stdin.readline +from collections import deque -    n = int(eval(input())) -    A = [list(map(int, input().split()))[::-1] for i in range(n)] -    day = 0 -    zan = [n - 1] * n -    zann = sum(zan) -    sumi = set(range(n)) -    while True: -        day += 1 -        kouho = list(sumi) -        sumi = set() -        for i in kouho: -            try: -                if A[A[i][-1] - 1][-1] - 1 == i: -                    sumi.add(i) -                    sumi.add(A[i][-1] - 1) -            except: -                pass -        for k in sumi: -            A[k].pop() -            zan[k] -= 1 -        if zann == sum(zan): +    N = int(eval(input())) +    A = [deque()] + [deque(list(map(int, input().split()))) for _ in range(N)] +    Days = [0] * (N + 1) +    NOW = [0] * (N + 1) +    q = deque([i + 1 for i in range(N)]) +    while q: +        i = q.popleft() +        if not A[i]: +            continue +        a = A[i].popleft() +        if NOW[a] == i: +            Days[a] = Days[i] = max(Days[a], Days[i]) + 1 +            q.append(i) +            q.append(a) +            NOW[a] = NOW[i] = 0 +        else: +            NOW[i] = a +    for i in range(1, N + 1): +        if A[i]: -            exit(0) -        zann = sum(zan) -        if zann == 0: -            print(day) -            exit(0) +            return +    print((max(Days)))