CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
 
a = {i+1: list(map(int, input().split())) for i in range(n)}
 
ret = 1
while True:
    pair = []
    for k, v in list(a.items()):
        if a[v[0]][0] == k:
            pair.append(k)
    for p in pair:
        if len(a[p]) > 1:
            a[p] = a[p][1:]
        else:
            a.pop(p)
    if len(a) == 0:
        break
    if len(pair) == 0:
        ret = -1
        break
    ret = ret + 1
 
print(ret)

# Optimized solution
n = int(eval(input()))
 
a = {i+1: list(map(int, input().split())) for i in range(n)}
ret = 1
pair = []
keys = list(a.keys())
while True:
    for k in keys:
        v = a[k][0]
        if a[v][0] == k:
            pair.append(k)
            if v not in keys:
                pair.append(v)
    for p in pair:
        if len(a[p]) > 1:
            a[p] = a[p][1:]
        else:
            a.pop(p)
    if len(a) == 0:
        break
    if len(pair) == 0:
        ret = -1
        break
    ret = ret + 1
    keys = [k for k in pair if k in a]
    pair = []
 
print(ret)
# Original solution
import sys
readline = sys.stdin.readline
from collections import deque
N = int(readline())
G = [[] for i in range(N*N)]
deg = [0]*(N*N)
day = [0]*(N*N)
F = set()
for p in range(N):
    *A, = list(map(int, readline().split()))
    for k in range(N-1):
        A[k] -= 1
    prv = A[0]+p*N if A[0] < p else p+A[0]*N
    F.add(prv)
    for k in range(1, N-1):
        i = A[k]+p*N if A[k] < p else p+A[k]*N
        G[prv].append(i)
        deg[i] += 1
        prv = i
que = deque()
for i in F:
    if deg[i] == 0:
        que.append(i)
        day[i] = 1
rest = N*(N-1)//2
while que:
    v = que.popleft()
    rest -= 1
    d = day[v]
    for w in G[v]:
        deg[w] -= 1
        if deg[w] == 0:
            day[w] = day[v] + 1
            que.append(w)
if rest > 0:
    print((-1))
else:
    print((max(day)))

# Optimized solution
import sys
from collections import deque
def solve():
    readline = sys.stdin.readline
    N = int(readline())
    G = [[] for i in range(N*N)]
    deg = [0]*(N*N)
    day = [0]*(N*N)
    F = set()
    for p in range(N):
        *A, = list(map(int, readline().split()))
        for k in range(N-1):
            A[k] -= 1
        prv = A[0]+p*N if A[0] < p else p+A[0]*N
        F.add(prv)
        for a in A[1:]:
            i = a+p*N if a < p else p+a*N
            G[prv].append(i)
            deg[i] += 1
            prv = i
    que = deque()
    for i in F:
        if deg[i] == 0:
            que.append(i)
            day[i] = 1
    rest = N*(N-1)//2
    pop = que.popleft
    push = que.append
    while que:
        v = pop()
        rest -= 1
        d = day[v]+1
        for w in G[v]:
            deg[w] -= 1
            if deg[w] == 0:
                day[w] = d
                push(w)
    if rest > 0:
        print((-1))
    else:
        print((d-1))
solve()
# Original solution
from collections import deque
import sys
input=sys.stdin.readline
def main():
  a=int(eval(input()))
  b=[[]for i in range(a)]
  for i in range(a):
    b[i]=deque(list(map(int,input().split())))
  c=[[]for i in range(a)]
  ans=0
  d=[i for i in range(a)]
  while d:
    nextd=[]
    f=[0 for i in range(a)]
    ans+=1
    T=0
    for i in d:
      if f[i]==0 and len(b[i])>0:
        e=b[i][0]-1
        if e in c[i]:
          c[i].pop(c[i].index(e))
          T=1
          b[e].popleft()
          b[i].popleft()
          if len(b[i])>0:
            nextd.append(i)
          if len(b[e])>0:
            nextd.append(e)
          f[i]=1
          f[e]=1
        else:
          c[e].append(i)
    if T==0:
      print((-1))
      exit()
    d=nextd
      
      
  print(ans)  
if __name__ == '__main__':  
  main()      

        




# Optimized solution
from collections import deque
import sys
input=sys.stdin.readline
def main():
  a=int(eval(input()))
  b=[[]for i in range(a)]
  for i in range(a):
    b[i]=deque(list(map(int,input().split())))
  c=[[]for i in range(a)]
  ans=0
  d=[i for i in range(a)]
  while d:
    nextd=[]
    f=set()
    ans+=1
    T=0
    for i in d:
      if i not in f and len(b[i])>0:
        e=b[i][0]-1
        if e in c[i]:
          c[i].pop(c[i].index(e))
          T=1
          b[e].popleft()
          b[i].popleft()
          if len(b[i])>0:
            nextd.append(i)
          if len(b[e])>0:
            nextd.append(e)
          f|={i,e}
        else:
          c[e].append(i)
    if T==0:
      print((-1))
      exit()
    d=nextd
      
      
  print(ans)  
if __name__ == '__main__':  
  main()
# Original solution
from collections import defaultdict, deque
outs = defaultdict(list)
ins = defaultdict(int)

def get_ab(h):
    return h // 10000, h % 10000

def get_hash(a, b):
    if a >= b:
        return 10000*a + b
    else:
        return 10000*b + a
v = []
N = int(eval(input()))
for i in range(N):
    nums = list(map(int, input().split()))
    pred = get_hash(i+1, nums[0])
    v.append(pred)
    for j in range(len(nums)-1):
        now = get_hash(i+1, nums[j+1])
        outs[pred].append(now)
        ins[now] += 1
        pred = now

v = set(v)
q = deque(v1 for v1 in v if ins[v1] == 0)
res = []
ans = 0
before = []
while q:
    v1 = q.popleft()
    res.append(v1)
    a, b = get_ab(v1)
    if (a in before) or (b in before):
        before = [a, b]
        ans += 1
    else:
        before.append(a)
        before.append(b)
    for v2 in outs[v1]:
        ins[v2] -= 1
        if ins[v2] == 0:
            q.append(v2)
if len(res) < (N*(N-1)) // 2:
    print((-1))
else:
    print((ans+1))


# Optimized solution
from collections import defaultdict, deque
outs = defaultdict(list)
ins = defaultdict(int)

def get_ab(h):
    return h // 10000, h % 10000

def get_hash(a, b):
    if a >= b:
        return 10000*a + b
    else:
        return 10000*b + a
v = []
N = int(eval(input()))
for i in range(N):
    nums = list(map(int, input().split()))
    pred = get_hash(i+1, nums[0])
    if pred not in v:
        v.append(pred)
    for j in range(len(nums)-1):
        now = get_hash(i+1, nums[j+1])
        outs[pred].append(now)
        ins[now] += 1
        pred = now

q = deque(v1 for v1 in v if ins[v1] == 0)
res = []
ans = 0
before = []
while q:
    v1 = q.popleft()
    res.append(v1)
    a, b = get_ab(v1)
    if (a in before) or (b in before):
        before = [a, b]
        ans += 1
    else:
        before.append(a)
        before.append(b)
    for v2 in outs[v1]:
        ins[v2] -= 1
        if ins[v2] == 0:
            q.append(v2)
if len(res) < (N*(N-1)) // 2:
    print((-1))
else:
    print((ans+1))
# Original solution
from collections import deque
import copy

n = int(eval(input()))
a = [deque([int(x) - 1 for x in input().split()]) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0

def check(p, q):
    if not len(a[p]):
        return
    o = a[p][0]
    if p == a[o][0]:
        if p > o: p, o = o, p
        if not (p, o) in q:
            q.append((p, o))

fin = []
for p in range(n):
    if not p in fin:
        o = a[p][0]
        if p == a[o][0]:
            q.append((p, o))
            fin += [p, o]

while q:
    day += 1
    pre_q = copy.copy(q)
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)

if all(map(len, a)):
    print((-1))
else:
    print(day)

# Optimized solution
from collections import deque
import copy, sys

input = sys.stdin.readline
n = int(eval(input()))
a = [deque([int(x) - 1 for x in input().split()]) for _ in range(n)]
q = deque()
pre_q = deque()
day = 0

def check(p, q):
    if not len(a[p]):
        return
    o = a[p][0]
    if p == a[o][0]:
        if p > o: p, o = o, p
        if not (p, o) in q:
            q.append((p, o))

fin = []
for p in range(n):
    if not p in fin:
        o = a[p][0]
        if p == a[o][0]:
            q.append((p, o))
            fin += [p, o]

while q:
    day += 1
    pre_q = copy.copy(q)
    q.clear()
    for (p, o) in pre_q:
        a[p].popleft()
        a[o].popleft()
        check(p, q)
        check(o, q)

if all(map(len, a)):
    print((-1))
else:
    print(day)
# Original solution
import queue


def make_state(N):
    state = [[-1 for j in range(N+1)] for i in range(N+1)]
    min_state = 0
    s = min_state
    for i in range(1, N+1):
        for j in range(1, i):
            state[i][j] = s
            state[j][i] = s
            s += 1
    return state


def solve(N, A):
    state = make_state(N)
    max_state = N * (N-1) // 2
    n_prev_states = [0 for i in range(max_state)]
    next_states = [set() for i in range(max_state)]
    for i in range(1, N+1):
        for k in range(N-2):
            ps = state[i][A[i][k]]
            ns = state[i][A[i][k+1]]
            n_prev_states[ns] += 1
            next_states[ps].add(ns)
    
    today_states = queue.Queue()
    for s in range(max_state):
        if n_prev_states[s] == 0:
            today_states.put_nowait(s)
    
    n_days = 0
    n_states_left = max_state
    while today_states.qsize() > 0:
        n_days += 1
        n_today_states = today_states.qsize()
        if n_today_states == 0:
            break
        for _ in range(n_today_states):
            s = today_states.get_nowait()
            n_states_left -= 1
            for ns in next_states[s]:
                n_prev_states[ns] -= 1
                if n_prev_states[ns] == 0:
                    today_states.put_nowait(ns)
    return n_days if n_states_left == 0 else -1


def main():
    N = int(eval(input()))
    A = list()
    A.append([])
    for i in range(N):
        a = list(map(int, input().strip().split()))
        A.append(a)
    print((solve(N, A)))


if __name__ == "__main__":
    main()


# Optimized solution
def make_state(N):
    state = [[-1 for j in range(N+1)] for i in range(N+1)]
    min_state = 0
    s = min_state
    for i in range(1, N+1):
        for j in range(1, i):
            state[i][j] = s
            state[j][i] = s
            s += 1
    return state


def solve(N, A):
    state = make_state(N)
    max_state = N * (N-1) // 2
    n_prev_states = [0 for i in range(max_state)]
    next_states = [set() for i in range(max_state)]
    for i in range(1, N+1):
        for k in range(N-2):
            ps = state[i][A[i][k]]
            ns = state[i][A[i][k+1]]
            n_prev_states[ns] += 1
            next_states[ps].add(ns)
    
    today_states = set()
    for s in range(max_state):
        if n_prev_states[s] == 0:
            today_states.add(s)
    
    n_days = 0
    n_states_left = max_state
    while len(today_states) > 0:
        n_days += 1
        if len(today_states) == 0:
            break
        n_states_left -= len(today_states)
        nextday_states = set()
        for s in today_states:
            for ns in next_states[s]:
                n_prev_states[ns] -= 1
                if n_prev_states[ns] == 0:
                    nextday_states.add(ns)
        today_states = nextday_states
    return n_days if n_states_left == 0 else -1


def main():
    N = int(eval(input()))
    A = list()
    A.append([])
    for i in range(N):
        a = list(map(int, input().strip().split()))
        A.append(a)
    print((solve(N, A)))


if __name__ == "__main__":
    main()
# Original solution
from collections import defaultdict, deque
n=int(eval(input()))
es=[]

for i in range(n):
    *A,=list(map(int,input().split()))
    for j in range(n-2):
        a=A[j]
        x,y=min(a-1,i),max(a-1,i)
        ith=((n-x)+(n-1))*x//2-1+(y-x)
        b=A[j+1]
        x,y=min(b-1,i),max(b-1,i)
        jth=((n-x)+(n-1))*x//2-1+(y-x)
        es.append([ith, jth])
#print(es)
v=n*(n-1)//2
outs = defaultdict(list)
ins = defaultdict(int)
for i,j in es:
    outs[i].append(j)
    ins[j] += 1
#print(outs, ins)
q = deque((v1,0) for v1 in range(n*(n-1)//2) if ins[v1] == 0)
res = []
while q:
    v1,cnt = q.popleft()
    res.append((v1,cnt))
    for v2 in outs[v1]:
        ins[v2] -= 1
        if ins[v2] == 0:
            q.append((v2,cnt+1))

x=len(res)
if x!=n*(n-1)//2:
    print((-1))
else:
    print((res[-1][1]+1))

# Optimized solution
from collections import defaultdict, deque
n=int(eval(input()))
v=n*(n-1)//2
es=[[] for _ in range(v)]

for i in range(n):
    *A,=list(map(int,input().split()))
    for j in range(n-2):
        a=A[j]
        x,y=min(a-1,i),max(a-1,i)
        ith=((n-x)+(n-1))*x//2-1+(y-x)
        b=A[j+1]
        x,y=min(b-1,i),max(b-1,i)
        jth=((n-x)+(n-1))*x//2-1+(y-x)
        es[ith].append(jth)

outs = defaultdict(list)
ins = defaultdict(int)

for v1 in range(v):
    for v2 in es[v1]:
        outs[v1].append(v2)
        ins[v2] += 1

q = deque((v1,0) for v1 in range(v) if ins[v1] == 0)
res = []
while q:
    v1,cnt = q.popleft()
    res.append((v1,cnt))
    for v2 in outs[v1]:
        ins[v2] -= 1
        if ins[v2] == 0:
            q.append((v2,cnt+1))
x=len(res)
if x!=v:
    print((-1))
else:
    print((res[-1][1]+1))
# Original solution
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict, Counter, deque
    from copy import copy

    n = int(readline())

    edge = defaultdict(list)
    count = Counter()

    for x in range(1, n + 1):
        a = list(map(int, readline().split()))
        for j in range(n - 2):
            y = a[j]
            z = a[j + 1]
            uf, us = min(x, y), max(x, y)
            vf, vs = min(x, z), max(x, z)
            edge[(uf, us)].append((vf, vs))
            count[(uf, us)] += 0
            count[(vf, vs)] += 1

    ans = 0
    rem = n * (n - 1) // 2
    que = deque()
    for key, val in list(count.items()):
        if val == 0:
            que.append(key)
    que_tomorrow = deque()

    while rem:
        ans += 1
        if not que:
            print((-1))
            return
        while que:
            cur_match = que.popleft()
            for nx_match in edge[cur_match]:
                count[nx_match] -= 1
                if count[nx_match] == 0:
                    que_tomorrow.append(nx_match)
            rem -= 1
        que = copy(que_tomorrow)
        que_tomorrow.clear()

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    def calc_id(x, y):
        return min(x, y) * 10007 + max(x, y)

    from collections import defaultdict, Counter, deque
    from copy import copy

    n = int(readline())

    edge = defaultdict(list)
    count = Counter()

    for x in range(1, n + 1):
        a = list(map(int, readline().split()))
        count[calc_id(x, a[0])] += 0
        for j in range(n - 2):
            y = a[j]
            z = a[j + 1]
            u_id = calc_id(x, y)
            v_id = calc_id(x, z)
            edge[u_id].append(v_id)
            count[v_id] += 1

    ans = 0
    rem = n * (n - 1) // 2
    que = deque()
    for key, val in list(count.items()):
        if val == 0:
            que.append(key)
    que_tomorrow = deque()

    while rem:
        ans += 1
        if not que:
            print((-1))
            return
        while que:
            cur_match = que.popleft()
            for nx_match in edge[cur_match]:
                count[nx_match] -= 1
                if count[nx_match] == 0:
                    que_tomorrow.append(nx_match)
            rem -= 1
        que = copy(que_tomorrow)
        que_tomorrow.clear()

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
n=int(eval(input()));a=[0]*n;l=[list(map(int,input().split()))for i in a];d=1;k=n*~-n
while sum(a)<k and d<1e4:
 L=[1]*n;y=0;d+=1
 for i in range(n):
  if a[i]<n-1:
   x=l[i][a[i]]-1
   if l[x][a[x]]*L[x]*L[i]==i+1:a[i]+=1;a[x]+=1;L[i]=L[x]=0;y=1
print(([k//2,d*y-1][d*y<1e4]))

# Optimized solution
n=int(eval(input()));a=[0]*n;l=[list(map(int,input().split()))for i in a];d=1;k=n*~-n
while sum(a)<k and d<1e4:
 L=[1]*n;y=0;d+=1
 for i in range(n):
  if-~a[i]-n:
   x=l[i][a[i]]-1
   if l[x][a[x]]*L[x]*L[i]==i+1:a[i]+=1;a[x]+=1;L[i]=L[x]=0;y=1
print(([k//2,d*y-1][d*y<1e4]))
# Original solution
from collections import deque
n=int(eval(input()))
lis=[[] for i in range(n*(n-1)//2)]
num=[-1]*(n**2)
cnt=-1
for x in range(n**2):
	if x//n<x%n:
		cnt+=1
		num[x]=cnt
for i in range(n):
	a=list(map(int,input().split()))
	for j in range(n-2):
		x=a[j]-1
		y=a[j+1]-1
		px=num[min(i*n+x,x*n+i)]
		py=num[min(i*n+y,y*n+i)]
		lis[px].append(py)
node_in=[0]*(n*(n-1)//2)
for i in range(n*(n-1)//2):
	for x in lis[i]:
		node_in[x]+=1
zero=deque([])
days=[10**9]*(n*(n-1)//2)
for i in range(n*(n-1)//2):
	if node_in[i]==0:
		zero.append(i)
		days[i]=1
while zero:
	r=zero.popleft()
	for x in lis[r]:
		node_in[x]-=1
		if node_in[x]==0:
			zero.append(x)
			days[x]=days[r]+1
if any([x==10**9 for x in days]):
	print((-1))
else:
	print((max(days)))


# Optimized solution
def main():
	from collections import deque
	n=int(eval(input()))
	battle=n*(n-1)//2
	lis=[[] for i in range(battle)]
	num=[-1]*(n**2)
	cnt=-1
	for x in range(n**2):
		if x//n<x%n:
			cnt+=1
			num[x]=cnt
	for i in range(n):
		a=list(map(int,input().split()))
		for j in range(n-2):
			x=a[j]-1
			y=a[j+1]-1
			px=num[min(i*n+x,x*n+i)]
			py=num[min(i*n+y,y*n+i)]
			lis[px].append(py)
	node_in=[0]*(battle)
	for i in range(battle):
		for x in lis[i]:
			node_in[x]+=1
	zero=deque([])
	days=[10**9]*(battle)
	for i in range(battle):
		if node_in[i]==0:
			zero.append(i)
			days[i]=1
	while zero:
		r=zero.popleft()
		for x in lis[r]:
			node_in[x]-=1
			if node_in[x]==0:
				zero.append(x)
				days[x]=days[r]+1
	if 10**9 in days:
		print((-1))
	else:
		print((max(days)))
if __name__ == '__main__':
	main()
# Original solution
from collections import deque

N=int(eval(input()))
A=[list(map(int,input().split())) for _ in range(N)]
A=list([deque(x) for x in A])
#print(A)

ans=0

L=[]
for i in range(N):
  L.append(len(A[i]))
#print(L)

while 1:
  P=[0]*N
  sw=0
  C=[]
  for i in range(N):
    if L[i]:
      if i+1==A[A[i][0]-1][0]:
        P[i]=1
        C.append(i)
        sw=1
  #print(P)
  if sw:
    for c in C:
      if P[c]:
        A[c].popleft()
        L[c]-=1
    ans+=1
  else:
    tmp=0
    for a in A:
      tmp+=len(a)
    if tmp!=0:
      ans=-1
    break
print(ans)
    

# Optimized solution
import sys
input = sys.stdin.buffer.readline

N = int(eval(input()))
A = [[]] + [list(map(int, input().split()))[::-1] for _ in range(N)]

pair = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

stack = []
for i in range(1, N + 1):
  a = A[i].pop()
  pair[i][a] += 1
  if pair[i][a] == pair[a][i] == 1:
    stack.append(i)
    stack.append(a)

r = []
answer = 1
while stack:
  while stack:
    i = stack.pop()
    if A[i]:
      a = A[i].pop()
      pair[i][a] += 1
      if pair[i][a] == pair[a][i] == 1:
        r.append(i)
        r.append(a)
  stack = r
  if r:
    answer += 1
  r = []
  
flag = 1
for i in range(1, N + 1):
  if A[i]:
    flag = 0
    break
if flag:
  print(answer)
else:
  print((-1))
# Original solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(eval(input()))
    c = [[int(i)-1 for i in input().split()] for j in range(N)]
    from collections import defaultdict
    edge = defaultdict(set)
    deg = defaultdict(int)
    for i, A in enumerate(c):
        for j in range(N-2):
            if i < A[j]:
                if i < A[j+1]:
                    edge[(i, A[j])].add((i, A[j+1]))
                    deg[(i, A[j+1])] += 1
                else:
                    edge[(i, A[j])].add((A[j+1], i))
                    deg[(A[j+1], i)] += 1
            else:
                if i < A[j+1]:
                    edge[(A[j], i)].add((i, A[j+1]))
                    deg[(i, A[j+1])] += 1
                else:
                    edge[(A[j], i)].add((A[j+1], i))
                    deg[(A[j+1], i)] += 1

    from collections import deque
    que = deque()
    dist = defaultdict(int)
    for i in range(N):
        for j in range(i+1, N):
            if deg[(i, j)] == 0:
                que.append((i, j))  # root
                dist[(i, j)] = 1
    while que:
        u = que.popleft()
        for v in edge[u]:
            deg[v] -= 1
            dist[v] = max(dist[v], dist[u] + 1)
            if deg[v] == 0:
                que.append(v)

    if any(deg.values()):
        # 閉路検出
        print((-1))
    else:
        print((max(dist.values())))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(eval(input()))
    c = [[int(i) for i in input().split()] for j in range(N)]
    from collections import defaultdict
    edge = defaultdict(set)
    deg = defaultdict(int)
    for i, A in enumerate(c, start=1):
        for j in range(N-2):
            if i < A[j]:
                if i < A[j+1]:
                    edge[i*N+A[j]].add(i*N+A[j+1])
                    deg[i*N+A[j+1]] += 1
                else:
                    edge[i*N+A[j]].add(A[j+1]*N+i)
                    deg[A[j+1]*N+i] += 1
            else:
                if i < A[j+1]:
                    edge[A[j]*N+i].add(i*N+A[j+1])
                    deg[i*N+A[j+1]] += 1
                else:
                    edge[A[j]*N+i].add(A[j+1]*N+i)
                    deg[A[j+1]*N+i] += 1
    from collections import deque
    que = deque()
    dist = defaultdict(int)
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            # print(i, j, i*N+j)
            if deg[i*N+j] == 0:
                que.append(i*N+j)  # root
                dist[i*N+j] = 1
    while que:
        u = que.popleft()
        for v in edge[u]:
            deg[v] -= 1
            dist[v] = max(dist[v], dist[u] + 1)
            if deg[v] == 0:
                que.append(v)

    if any(deg.values()):
        # 閉路検出
        print((-1))
    else:
        print((max(dist.values())))


if __name__ == '__main__':
    main()
# Original solution
def main():
    N = int(eval(input()))
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        a = [i-1 for i in a]
        a.reverse()
        A.append(a)
    t = []
    d = 0
    n = set(range(N))
    while True:
        p = 0
        l = set()
        d += 1
        for i in n:
            if A[i] == [] or i in l:
                continue
            j = A[i][-1]
            if A[i][-1] not in l and A[j][-1] == i:
                l.add(i)
                l.add(A[i][-1])
                A[j] = A[j][:-1]
                A[i] = A[i][:-1]
                p += 1
        n = l
        if p == 0:
            print((-1))
            break
        if sum([len(i) for i in A]) == 0:
            print(d)
            break
main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = []
    for _ in range(N):
        a = list(map(int, input().split()))
        a = [i-1 for i in a]
        a.reverse()
        A.append(a)
    t = N * (N - 1) / 2
    d = 0
    n = set(range(N))
    while True:
        p = 0
        l = set()
        d += 1
        for i in n:
            if A[i] == [] or i in l:
                continue
            j = A[i][-1]
            if A[i][-1] not in l and A[j][-1] == i:
                l.add(i)
                l.add(A[i][-1])
                A[j].pop() 
                A[i].pop()
                t -= 1
                p += 1
        n = l
        if t == 0:
            print(d)
            break
        if p == 0:
            print((-1))
            break
main()