Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        ans += 1

if __name__ == '__main__':
    print(solve())


Fixed submission:
import sys,time
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    start = time.time()

    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        now = time.time() - start
        if now > 1.6:
            return N*(N-1)//2

        ans += 1

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


class Searchable_BIT():
    def __init__(self,N):
        self.N = N
        self.node = [0]*(self.N+1)
        self.cnt = 0

    def add(self,a): # 要素 x を追加
        x = a
        self.cnt += 1
        while x <= self.N:
            self.node[x] += 1
            x += x & -x

    def delete(self,x): # 要素 x を削除
        self.cnt -= 1
        while x <= self.N:
            self.node[x] -= 1
            x += x & -x

    def count(self,x): # x以下の要素数
        tmp = 0
        while x > 0:
            tmp += self.node[x]
            x -= x & -x
        return tmp

    def get_maxval(self):
        return self.get_lower_i(self.cnt)

    def get_lower_i(self,i): # i 番目に小さい要素を取得
        NG = -1
        OK = self.N
        while OK-NG > 1:
            mid = (OK+NG)//2
            #print(OK,NG,self.count(mid))
            if self.count(mid) >= i:
                OK = mid
            else:
                NG = mid
        return OK


def calc(x,y):
    return x*(N-1) + y

N = inp()
AA = [inpl() for _ in range(N)]

itrs = [0]*N

ans = 0
cnt = 0

while True:
    next = set()
    for x,itr in enumerate(itrs):
        if itr < N-1:
            t = AA[x][itr] - 1
            if AA[t][itrs[t]]-1 == x:
                next.add(x)

    for n in next:
        itrs[n] += 1
        if itrs[n] == N-1:
            cnt += 1

    ans += 1

    if cnt == N:
        print(ans)
        exit()

    if len(next) == 0:
        print(-1)
        exit()


Fixed submission:
import sys,time
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    start = time.time()

    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        now = time.time() - start
        if now > 1.6:
            return N*(N-1)//2

        ans += 1

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


#ベルマンフォード(始点と終点が決まってる時)
def BellmanFord(Start,Goal):
    Costs=[INF]*N
    Costs[Start] = 0
    upd8s = [True]*N
    for i in range(2*N): #2N回ループ(負閉路の検出までみる)
        will_upd8s = [False]*N
        for s in range(N):
            if not upd8s[s]: continue    #前回更新してないので見ない
            for t,c in lines[s]:
                if c + Costs[s] < Costs[t]:
                    Costs[t] = Costs[s]+c
                    will_upd8s[t] = True #更新した点だけ次に見る
                    if i >= N:
                        return -INF

        if i == N-1: #Nループ目のGoalのCostを記録
            tmp = Costs[Goal]

        upd8s = will_upd8s[:]

    if tmp != Costs[Goal]:
        return -INF
    else:
        return Costs[Goal]


def calc(x,y):
    return x*(N-1) + y

N = inp()
AAA = [inpl() for _ in range(N)]


next = [[-1]*N for _ in range(N)]
for x in range(N):
    for y,A in enumerate(AAA[x]):
        next[x][A-1] = y

lines = defaultdict(set)
S = N*(N-1)+1
T = S + 1

for x in range(N):
    lines[S].add((calc(x,0),0))
    lines[calc(x,N-2)].add((T,0))
    for y in range(N-1):
        tx = AAA[x][y] - 1
        ty = next[tx][x]
        s = calc(x,y)
        lines[s].add((calc(tx,ty),0))
        if y != N-2:
            lines[s].add((calc(x,y+1),-1))

N = T+1
ret = -BellmanFord(S,T)

if ret == INF:
    print(-1)
else:
    print(ret+1)


Fixed submission:
import sys,time
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    start = time.time()

    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        now = time.time() - start
        if now > 1.6:
            return N*(N-1)//2

        ans += 1

if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


#ベルマンフォード(始点と終点が決まってる時)
def BellmanFord(Start,Goal):
    Costs=[INF]*N
    Costs[Start] = 0
    upd8s = [True]*N
    for i in range(2*N): #2N回ループ(負閉路の検出までみる)
        will_upd8s = [False]*N
        for s in range(N):
            if not upd8s[s]: continue    #前回更新してないので見ない
            for t,c in lines[s]:
                if c + Costs[s] < Costs[t]:
                    Costs[t] = Costs[s]+c
                    will_upd8s[t] = True #更新した点だけ次に見る
                    if i >= N:
                        Costs[t] = -INF

        if i == N-1: #Nループ目のGoalのCostを記録
            tmp = Costs[Goal]

        upd8s = will_upd8s[:]

    if tmp != Costs[Goal]:
        return -INF
    else:
        return Costs[Goal]


def calc(x,y):
    return x*(N-1) + y

N = inp()
AAA = [inpl() for _ in range(N)]


next = [[-1]*N for _ in range(N)]
for x in range(N):
    AA = AAA[x]
    for y,A in enumerate(AA):
        next[x][A-1] = y

lines = defaultdict(set)

for x in range(N):
    for y in range(N-1):
        tx = AAA[x][y] - 1
        ty = next[tx][x]
        lines[calc(x,y)].add((calc(tx,ty),0))

for x in range(N):
    for y in range(N-2):
        lines[calc(x,y)].add((calc(x,y+1),-1))

S = N*(N-1)+1
T = S + 1

for x in range(N):
    lines[S].add((calc(x,0),0))
    lines[calc(x,N-2)].add((T,0))

N = T+1
ret = -BellmanFord(S,T)

if ret == INF:
    print(-1)
else:
    print(ret+1)


Fixed submission:
import sys,time
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

def solve():
    start = time.time()

    N = inp()
    AA = [inpl() for _ in range(N)]
    itrs = [0]*N
    ans = cnt = 0

    while True:
        next = set()
        for x,itr in enumerate(itrs):
            if itr < N-1:
                t = AA[x][itr] - 1
                if AA[t][itrs[t]]-1 == x:
                    next.add(x)

        for n in next:
            itrs[n] += 1
            if itrs[n] == N-1:
                cnt += 1

        if len(next) == 0:
            for x in itrs:
                if x != N-1:
                    return -1
            return ans

        now = time.time() - start
        if now > 1.6:
            return N*(N-1)//2

        ans += 1

if __name__ == '__main__':
    print(solve())