Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 21:55:48 2019

@author: Yamazaki Kenichi
"""


N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]
yesterday = [i for i in range(N)]

ans = 0
counta = 0
while counta < N:
    today = [True for i in range(N)]
    today_p = []
    for i in yesterday:
        if today[i]:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and today[vs]:
                T[i] += 1
                T[vs] += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today[i], today[vs] = False, False
                today_p.extend([i,vs])
    yesterday = today_p
#        print(i,vs,today,T)
    if len(today_p) == 0:
        ans = -1
        break
    ans += 1
print(ans)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 21:55:48 2019

@author: Yamazaki Kenichi
"""


N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]
yesterday = [i for i in range(N)]

ans = 0
counta = 0
while counta < N:
    today = set([])
    today_p = []
    for i in yesterday:
        if not i in today:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and not vs in today:
                T[i] += 1
                T[vs] += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today.add(i)
                today.add(vs)
                today_p.extend([i,vs])
    yesterday = today_p
#        print(i,vs,today,T)
    if len(today_p) == 0:
        ans = -1
        break
    ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 20:25:02 2019

@author: Yamazaki Kenichi
"""

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]

ans = 0
counta = 0
check, check2 = 0, 0
while counta < N:
    today = [True for i in range(N)]
    for i in range(N):
        if today[i]:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and today[vs]:
                T[i] += 1
                T[vs] += 1
                check2 += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today[i], today[vs] = False, False
#        print(i,vs,today,T)
    if check == check2:
        ans = -1
        break
    else:
        check = check2
    ans += 1
print(ans)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 21:55:48 2019

@author: Yamazaki Kenichi
"""


N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]
yesterday = [i for i in range(N)]

ans = 0
counta = 0
while counta < N:
    today = set([])
    today_p = []
    for i in yesterday:
        if not i in today:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and not vs in today:
                T[i] += 1
                T[vs] += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today.add(i)
                today.add(vs)
                today_p.extend([i,vs])
    yesterday = today_p
#        print(i,vs,today,T)
    if len(today_p) == 0:
        ans = -1
        break
    ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 20:25:02 2019

@author: Yamazaki Kenichi
"""

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]

ans = 0
counta = 0
while counta < N:
    today = [True for i in range(N)]
    for i in range(N):
        if today[i]:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and today[vs]:
                T[i] += 1
                T[vs] += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today[i], today[vs] = False, False
#        print(i,vs,today,T)
    flg = True
    for i in range(N):
        flg *= today[i]
    if flg :
        ans = -1
        break
    ans += 1
print(ans)

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  1 21:55:48 2019

@author: Yamazaki Kenichi
"""


N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

T = [0 for i in range(N)]
today = [True for i in range(N)]
yesterday = [i for i in range(N)]

ans = 0
counta = 0
while counta < N:
    today = set([])
    today_p = []
    for i in yesterday:
        if not i in today:
            vs = A[i][T[i]] - 1
            if A[vs][T[vs]] == i + 1 and not vs in today:
                T[i] += 1
                T[vs] += 1
                if T[i] == N - 1:
                    T[i] = 0
                    counta += 1
                if T[vs] == N - 1:
                    T[vs] = 0 
                    counta += 1
                today.add(i)
                today.add(vs)
                today_p.extend([i,vs])
    yesterday = today_p
#        print(i,vs,today,T)
    if len(today_p) == 0:
        ans = -1
        break
    ans += 1
print(ans)