Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())

dep = []

for i in range(N):

    dep.append([0] * N)


lis = []

for i in range(N):

    l1 = []

    for j in range(N):

        l1.append([])

    lis.append(l1)

A = []

for i in range(N):

    a = list(map(int,input().split()))
    A.append(a)

    for j in range(len(a)-1):

        lmi = min(i,a[j]-1)
        lma = max(i,a[j]-1)
        rmi = min(i,a[j+1]-1)
        rma = max(i,a[j+1]-1)

        lis[lmi][lma].append([rmi,rma])

Q = []

 
for i in range(N):

    if i + 1 == A[A[i][0]-1][0] and i + 1 < A[i][0]:
        Q.append([i,A[i][0]-1])
        dep[i][A[i][0]-1] = 1

#print (Q)
#print (lis)
flag = True
if len(Q) == 0:
    flag = False


ans = 1

while len(Q) > 0 and ans < N * (N-1) // 2 + 5:

    q = Q.pop(0)

    for nex in lis[q[0]][q[1]]:

        l = nex[0]
        r = nex[1]

        if dep[l][r] < dep[q[0]][q[1]] + 1:
            dep[l][r] = dep[q[0]][q[1]] + 1
            Q.append([l,r])

            ans = max(ans,dep[l][r])

if ans <= N * (N-1) // 2 and flag:
    print (ans)
else:
    print (-1)
        


Fixed submission:

N = int(input())

A = []

for i in range(N):

    a = list(map(int,input().split()))

    A.append(a)

Q = []
for i in range(N):

    if A[ A[i][0]-1 ][0] == i + 1 and A[i][0]-1 > i:
        Q.append([min(A[i][0]-1,i),max(A[i][0]-1,i),1])

day = 0

while len(Q) > 0:

    q = Q.pop(0)

    del A[q[0]][0]
    del A[q[1]][0]




    if len(A[q[0]]) > 0 and A[ A[q[0]][0] - 1 ][0] == q[0] + 1:
        Q.append([min(A[q[0]][0] - 1,q[0]),max(A[q[0]][0] - 1,q[0]),q[2]+1])

    if len(A[q[1]]) > 0 and A[ A[q[1]][0] - 1 ][0] == q[1] + 1:
        Q.append([min(A[q[1]][0] - 1,q[1]),max(A[q[1]][0] - 1,q[1]),q[2]+1])

    day = max(day,q[2])

flag = True

for a in A:
    if len(a) > 0:
        flag = False
        
if flag:
    print (day)
else:
    print (-1)

Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())

a = []

for i in range(N):

    A = list(map(int,input().split()))

    a.append(A)

day = 0
flag = True
end = 0

while end < N and day < N*(N-1)//2:

    day += 1

    Q = []
    flag = False

    for i in range(N):


        if len(a[i]) > 0  and a[ a[i][0]-1 ][0] == i + 1 and a[i][0] > i + 1:

            Q.append(a[i][0])
            Q.append(i + 1)

            flag = True

    if not flag:
        break

    #print (a,Q)

    for q in Q:
        del a[q-1][0]

        if len(a[q-1]) == 0:
            end += 1



if flag:
    print (day)
else:
    print (-1)
        
        


Fixed submission:

N = int(input())

A = []

for i in range(N):

    a = list(map(int,input().split()))

    A.append(a)

Q = []
for i in range(N):

    if A[ A[i][0]-1 ][0] == i + 1 and A[i][0]-1 > i:
        Q.append([min(A[i][0]-1,i),max(A[i][0]-1,i),1])

day = 0

while len(Q) > 0:

    q = Q.pop(0)

    del A[q[0]][0]
    del A[q[1]][0]




    if len(A[q[0]]) > 0 and A[ A[q[0]][0] - 1 ][0] == q[0] + 1:
        Q.append([min(A[q[0]][0] - 1,q[0]),max(A[q[0]][0] - 1,q[0]),q[2]+1])

    if len(A[q[1]]) > 0 and A[ A[q[1]][0] - 1 ][0] == q[1] + 1:
        Q.append([min(A[q[1]][0] - 1,q[1]),max(A[q[1]][0] - 1,q[1]),q[2]+1])

    day = max(day,q[2])

flag = True

for a in A:
    if len(a) > 0:
        flag = False
        
if flag:
    print (day)
else:
    print (-1)

Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())

a = []

for i in range(N):

    A = list(map(int,input().split()))

    a.append(A)

day = 0
flag = True
end = 0

while end < N:

    day += 1

    Q = []
    flag = False

    for i in range(N):


        if len(a[i]) > 0  and a[ a[i][0]-1 ][0] == i + 1 and a[i][0] > i + 1:

            Q.append(a[i][0])
            Q.append(i + 1)

            flag = True

    if not flag:
        break

    #print (a,Q)

    for q in Q:
        del a[q-1][0]

        if len(a[q-1]) == 0:
            end += 1



if flag:
    print (day)
else:
    print (-1)
        
        

Fixed submission:

N = int(input())

A = []

for i in range(N):

    a = list(map(int,input().split()))

    A.append(a)

Q = []
for i in range(N):

    if A[ A[i][0]-1 ][0] == i + 1 and A[i][0]-1 > i:
        Q.append([min(A[i][0]-1,i),max(A[i][0]-1,i),1])

day = 0

while len(Q) > 0:

    q = Q.pop(0)

    del A[q[0]][0]
    del A[q[1]][0]




    if len(A[q[0]]) > 0 and A[ A[q[0]][0] - 1 ][0] == q[0] + 1:
        Q.append([min(A[q[0]][0] - 1,q[0]),max(A[q[0]][0] - 1,q[0]),q[2]+1])

    if len(A[q[1]]) > 0 and A[ A[q[1]][0] - 1 ][0] == q[1] + 1:
        Q.append([min(A[q[1]][0] - 1,q[1]),max(A[q[1]][0] - 1,q[1]),q[2]+1])

    day = max(day,q[2])

flag = True

for a in A:
    if len(a) > 0:
        flag = False
        
if flag:
    print (day)
else:
    print (-1)