from collections import Counter,defaultdict,deque

from bisect import bisect_left

import sys,math,itertools,pprint,fractions,time

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))



st = time.time()

n = inp()

a = []

for i in range(n):

    b = inpl()

    a.append(deque(b))

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

res = 0

while True:

    res += 1

    now = zan

    s = set()

    for i in range(n):

        if i in s or a[i] == deque([]): continue

        end = True

        chk = a[i][0] - 1

        if not chk in s and a[chk][0] == i+1:

            zan -= 1

            if not zan: 

                print(res); quit()

            a[i].popleft(); a[chk].popleft()

            s.add(i); s.add(chk)

        # print(res,i,chk,a)

        nnn = time.time()

    if now == zan:

        print((-1))

        quit()

    if nnn - st > 1.7:

        print((n*(n-1)//2))

        quit()



            

from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions,copy

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))



n = inp()

a = []

for _ in range(n):

    tmp = inpl()

    tmp = [x-1 for x in tmp] + [-1]

    # print(tmp)

    a.append(deque(tmp))

md = [0] * n

nx = [-1] * n

for i in range(n):

    nx[i] = a[i].popleft()

q = deque(list(range(n)))



while q:

    x = q.popleft()

    y = nx[x]

    if nx[y] == x:

        d = max(md[x], md[y])

        md[x] = md[y] = d+1

        nx[x] = a[x].popleft()

        nx[y] = a[y].popleft()

        q.append(x); q.append(y)

if any(x for x in a):

    print((-1))

else:

    print((max(md)))
from collections import Counter, defaultdict, deque
from bisect import bisect_left
import sys, math, itertools, pprint, fractions, time

sys.setrecursionlimit(10**8)
mod = 10**9 + 7
INF = float("inf")


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


st = time.time()
n = inp()
a = []
for i in range(n):
    b = inpl()
    a.append(deque(b))
zan = n * (n - 1) // 2
res = 0
while True:
    res += 1
    now = zan
    s = set()
    for i in range(n):
        if i in s or a[i] == deque([]):
            continue
        end = True
        chk = a[i][0] - 1
        if not chk in s and a[chk][0] == i + 1:
            zan -= 1
            if not zan:
                print(res)
                quit()
            a[i].popleft()
            a[chk].popleft()
            s.add(i)
            s.add(chk)
        # print(res,i,chk,a)
        nnn = time.time()
    if now == zan:
        print((-1))
        quit()
    if nnn - st > 1.7:
        print((n * (n - 1) // 2))
        quit()

from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions, copy

sys.setrecursionlimit(10**8)
mod = 10**9 + 7
INF = float("inf")


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


n = inp()
a = []
for _ in range(n):
    tmp = inpl()
    tmp = [x - 1 for x in tmp] + [-1]
    # print(tmp)
    a.append(deque(tmp))
md = [0] * n
nx = [-1] * n
for i in range(n):
    nx[i] = a[i].popleft()
q = deque(list(range(n)))
while q:
    x = q.popleft()
    y = nx[x]
    if nx[y] == x:
        d = max(md[x], md[y])
        md[x] = md[y] = d + 1
        nx[x] = a[x].popleft()
        nx[y] = a[y].popleft()
        q.append(x)
        q.append(y)
if any(x for x in a):
    print((-1))
else:
    print((max(md)))

-from bisect import bisect_left -import sys, math, itertools, pprint, fractions, time +from heapq import heappop, heappush, heapify +import sys, bisect, math, itertools, fractions, copy -st = time.time() +for _ in range(n): +    tmp = inpl() +    tmp = [x - 1 for x in tmp] + [-1] +    # print(tmp) +    a.append(deque(tmp)) +md = [0] * n +nx = [-1] * n -    b = inpl() -    a.append(deque(b)) -zan = n * (n - 1) // 2 -res = 0 -while True: -    res += 1 -    now = zan -    s = set() -    for i in range(n): -        if i in s or a[i] == deque([]): -            continue -        end = True -        chk = a[i][0] - 1 -        if not chk in s and a[chk][0] == i + 1: -            zan -= 1 -            if not zan: -                print(res) -                quit() -            a[i].popleft() -            a[chk].popleft() -            s.add(i) -            s.add(chk) -        # print(res,i,chk,a) -        nnn = time.time() -    if now == zan: -        print((-1)) -        quit() -    if nnn - st > 1.7: -        print((n * (n - 1) // 2)) -        quit() +    nx[i] = a[i].popleft() +q = deque(list(range(n))) +while q: +    x = q.popleft() +    y = nx[x] +    if nx[y] == x: +        d = max(md[x], md[y]) +        md[x] = md[y] = d + 1 +        nx[x] = a[x].popleft() +        nx[y] = a[y].popleft() +        q.append(x) +        q.append(y) +if any(x for x in a): +    print((-1)) +else: +    print((max(md)))