Buggy python submission (verdict: Time Limit Exceeded):
# トポロジカルソートを使った例
def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict,deque
    import copy
    
    N = int(input())
    A = defaultdict(list)
    C = defaultdict(list)
    parent = deque()
    
    for i in range(1,N+1):
        tmp = list(map(int, input().split()))
        for j in range(1,len(tmp)):
            ii0 = min(i,tmp[j-1])
            jj0 = max(i,tmp[j-1])
            ii1 = min(i,tmp[j])
            jj1 = max(i,tmp[j])
            if A[(ii0,jj0)] == []:
                A[(ii0,jj0)] = [[],[]]
                C[(ii0,jj0)] = [[],[]]
            if A[(ii1,jj1)] == []:
                A[(ii1,jj1)] = [[],[]]
                C[(ii1,jj1)] = [[],[]]
            A[(ii0,jj0)][1].append((ii1,jj1))
            A[(ii1,jj1)][0].append((ii0,jj0))
    
    for i in range(1,N+1):
        for j in range(i+1,N+1):
            if A[(i,j)][0] == []:
                parent.append((i,j))
    #print(A)                        
    def toposort(node):
        #print(D)
        
        if D[node] == 1:
            print(-1)
            sys.exit()
        elif D[node] == 0:
            D[node] = 1
            for i in A[node][1]:
                toposort(i)
            D[node] = 2
            L.appendleft(node)
    
    L = deque()
    D = defaultdict(int)
    flag = 0
    for i in range(1,N+1):
      for j in range(i+1,N+1):
        toposort((i,j))
    
    
    B = defaultdict(int)
    mattan = []
    #print(L)
    for i in L:
        tmp = 0
        for j in A[i][0]:
            if tmp < B[j]:
                tmp = B[j]
        B[i] = tmp+1
        if A[i][1] == []:
            mattan.append(i)
    ans = 0
    
    for i in mattan:
        if B[i] > ans:
            ans = B[i]
    print(ans)
 
        
        
    
    
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# トポロジカルソートを使った例
def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict,deque
    import copy
    
    N = int(input())
    A = defaultdict(list)
    C = defaultdict(list)
    parent = deque()
    
    for i in range(1,N+1):
        tmp = list(map(int, input().split()))
        for j in range(1,len(tmp)):
            ii0 = min(i,tmp[j-1])
            jj0 = max(i,tmp[j-1])
            ii1 = min(i,tmp[j])
            jj1 = max(i,tmp[j])
            if A[(ii0,jj0)] == []:
                A[(ii0,jj0)] = [[],[]]
                C[(ii0,jj0)] = [[],[]]
            if A[(ii1,jj1)] == []:
                A[(ii1,jj1)] = [[],[]]
                C[(ii1,jj1)] = [[],[]]
            A[(ii0,jj0)][1].append((ii1,jj1))
            A[(ii1,jj1)][0].append((ii0,jj0))
    
    for i in range(1,N+1):
        for j in range(i+1,N+1):
            if A[(i,j)][0] == []:
                parent.append((i,j))
    #print(A)                        
    def toposort(node):
        #print(D)
        
        if D[node] == 1:
            print(-1)
            sys.exit()
        elif D[node] == 0:
            D[node] = 1
            for i in A[node][1]:
                toposort(i)
            D[node] = 2
            L.appendleft(node)
    
    L = deque()
    D = defaultdict(int)
    flag = 0
    for i in range(1,N+1):
      for j in range(i+1,N+1):
        toposort((i,j))
    
    
    B = defaultdict(int)
    mattan = []
    #print(L)
    for i in L:
        tmp = 0
        for j in A[i][0]:
            if tmp < B[j]:
                tmp = B[j]
        B[i] = tmp+1
        if A[i][1] == []:
            mattan.append(i)
    ans = 0
    
    for i in mattan:
        if B[i] > ans:
            ans = B[i]
    print(ans)

        
        
    
    
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# トポロジカルソートを使った例
def main():
    import sys
    input = sys.stdin.readline
    from collections import defaultdict,deque
    import copy
    
    N = int(input())
    A = defaultdict(list)
    C = defaultdict(list)
    parent = deque()
    
    for i in range(1,N+1):
        tmp = list(map(int, input().split()))
        for j in range(1,len(tmp)):
            ii0 = min(i,tmp[j-1])
            jj0 = max(i,tmp[j-1])
            ii1 = min(i,tmp[j])
            jj1 = max(i,tmp[j])
            if A[(ii0,jj0)] == []:
                A[(ii0,jj0)] = [[],[]]
                C[(ii0,jj0)] = [[],[]]
            if A[(ii1,jj1)] == []:
                A[(ii1,jj1)] = [[],[]]
                C[(ii1,jj1)] = [[],[]]
            A[(ii0,jj0)][1].append((ii1,jj1))
            A[(ii1,jj1)][0].append((ii0,jj0))
    
    for i in range(1,N+1):
        for j in range(i+1,N+1):
            if A[(i,j)][0] == []:
                parent.append((i,j))
    L = []
    tmpA = copy.deepcopy(A)
    
    while parent != deque():
        n = parent.popleft()
        L.append(n)
        for i in tmpA[n][1]:
            tmpA[i][0].remove(n)
            if tmpA[i][0] == []:
                parent.append(i)
        tmpA[n][1] = []
    
    if tmpA != C:
        print(-1)
        sys.exit()
    
    B = defaultdict(int)
    mattan = []
    #print(L)
    for i in L:
        tmp = 0
        for j in A[i][0]:
            if tmp < B[j]:
                tmp = B[j]
        B[i] = tmp+1
        if A[i][1] == []:
            mattan.append(i)
    ans = 0
    
    for i in mattan:
        if B[i] > ans:
            ans = B[i]
    print(ans)
    
        
        
    
    
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    D = []
    for i in range(1,N+1):
        D.append(i)
        
    while count < N*(N-1)//2 and flag == 0:
        flag = 1
        for i in D:
            if B[i] < N:
                j = A[i][B[i]]
                if A[j][B[j]] == i:
                    k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
                    C[i][B[i]] = k
                    C[j][B[j]] = k
                    B[i] += 1
                    if B[i] == N:
                        D.remove(i)
                    B[j] += 1
                    if B[j] == N:
                        D.remove(j)
                    count += 1
                    flag = 0
                    
    #print(C)
    ans = 0
    for i in C:
        if i[-1] > ans:
            ans = i[-1]
    if ans == 0:
        print(-1)
    else:
        print(ans)
    
    
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    while count < N*(N-1)//2 and flag == 0:
        flag = 1
        for i in range(1,N+1):
            if B[i] < N:
                j = A[i][B[i]]
                if A[j][B[j]] == i:
                    k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
                    C[i][B[i]] = k
                    C[j][B[j]] = k
                    B[i] +=  1
                    B[j] += 1
                    count += 1
                    flag = 0
                    
    #print(C)
    ans = 0
    for i in C:
        if i[-1] > ans:
            ans = i[-1]
    if ans == 0:
        print(-1)
    else:
        print(ans)
    
    
    
if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    ans = 0
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    while count < N*(N-1)//2 and  flag == 0:
        flag = 1
        for i in range(1,N+1):
            if B[i] < N:
                j = A[i][B[i]]
                if A[j][B[j]] == i:
                    k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
                    C[i][B[i]] = k
                    C[j][B[j]] = k
                    B[i] +=  1
                    B[j] += 1
                    count += 1
                    flag = 0
                    break
    #print(C)
    ans = 0
    for i in C:
        if i[-1] > ans:
            ans = i[-1]
    if ans == 0:
        print(-1)
    else:
        print(ans)
    
    
    
main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    
    N = int(input())
    A = []
    tmp = (0,)*(N+1)
    A.append(tmp)
    for i in range(N):
        tmp = (0,) + tuple(map(int, input().split()))
        A.append(tmp)
    B = [1]*(N+1)
    C = [[0]*N for i in range(N+1)]
    count = 0
    flag = 0
    
    #1試合目
    Q = []
    nextQ = []
    for i in range(1,N+1):
        j = A[i][B[i]]
        if A[j][B[j]] == i:
            ii = min(i,j)
            jj = max(i,j)
            if not (ii,jj) in nextQ:
                nextQ.append((ii,jj))
    
    day = 0    
    while count < N*(N-1)//2 and len(nextQ) >= 1:
        day += 1
        Q = nextQ
        nextQ = []
        for T in Q:
            i = T[0]
            j = T[1]
            k = max(C[i][B[i]-1], C[j][B[j]-1]) + 1
            C[i][B[i]] = k
            C[j][B[j]] = k
            B[i] += 1
            B[j] += 1
            count += 1
            if B[i] < N:
                I = A[i][B[i]]
                if A[I][B[I]] == i:
                    ii = min(i,I)
                    jj = max(i,I)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            if B[j] < N:
                J = A[j][B[j]]
                if A[J][B[J]] == j:
                    ii = min(j,J)
                    jj = max(j,J)
                    if not (ii,jj) in nextQ:
                        nextQ.append((ii,jj))
            
                    
    #print(C)
    if count < N*(N-1)//2:
        print(-1)
    else:
        print(day)
    
    
    
if __name__ == '__main__':
    main()