Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Runtime Error):
n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
g={(i,j):[] for i in range(n+1) for j in range(n+1) if i<j}
visit={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}
d={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}

inf=10**10

for i in range(1,n+1):
    last=(0,1)
    for j in list(map(int,input().split())):
        current=(min(i,j),max(i,j))
        g[last].append(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs((0,1)))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Runtime Error):
n=int(input())
g={(i,j):set() for i in range(n+1) for j in range(n+1) if i<j}
visit={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}
d={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}

inf=10**10

for i in range(1,n+1):
    last=(0,1)
    for j in list(map(int,input().split())):
        current=(min(i,j),max(i,j))
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
        
    if len(g[u])==0:
        return 0
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        ans=max(ans,1+dfs(v))
    
    visit[u]=2
    d[u]=ans
    return d[u]

print(dfs((0,1)))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
g={(i,j):set() for i in range(n+1) for j in range(n+1) if i<j}
visit={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}
d={(i,j):0 for i in range(n+1) for j in range(n+1) if i<j}

inf=10**10

for i in range(1,n+1):
    last=(0,1)
    for j in list(map(int,input().split())):
        current=(min(i,j),max(i,j))
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
        
    if len(g[u])==0:
        return 0
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        ans=max(ans,1+dfs(v))
    
    visit[u]=2
    d[u]=ans
    return d[u]

print(dfs((0,1)))

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n=int(input())
a=[0]+[list(map(int,sys.stdin.readline().strip().split()[::-1])) for _ in range(n)]

pairs=n*(n-1)//2
ans=0

while pairs>0:
    ans+=1
    tpairs=0
    dic={}

    for i in range(1,n+1):
        if len(a[i])==0:
            continue
        lasta=a[i][-1]
        p=(min(i,lasta),max(i,lasta))

        if p in dic:
            dic[p]+=1
        else:
            dic[p]=1

    for (i,j),v in dic.items():
        if v==1:
            continue

        a[i].pop()
        a[j].pop()
        tpairs+=1
    
    if tpairs==0:
        print(-1)
        break

    pairs-=tpairs
else:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter

n=int(input())
a=[0]+[list(map(int,sys.stdin.readline().strip().split()[::-1])) for _ in range(n)]

pairs=n*(n-1)//2
ans=0

while pairs>0:
    ans+=1
    tpairs=0
    x=[]

    for i in range(1,n+1):
        if len(a[i])==0:
            continue
        lasta=a[i][-1]
        x.append((min(i,lasta),max(i,lasta)))
    
    cnt=Counter(x)

    for (i,j),v in cnt.most_common():
        if v==1:
            break

        a[i].pop()
        a[j].pop()
        tpairs+=1
    
    if tpairs==0:
        print(-1)
        break

    pairs-=tpairs
else:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter

n=int(input())
a=[0]+[list(map(int,sys.stdin.readline().strip().split()[::-1])) for _ in range(n)]

pairs=n*(n-1)//2
ans=0

while pairs>0:
    ans+=1
    tpairs=0
    x=[]

    for i in range(1,n+1):
        if len(a[i])==0:
            continue
        x.append((min(i,a[i][-1]),max(i,a[i][-1])))
    
    cnt=Counter(x)

    for (i,j),v in cnt.most_common():
        if v==1:
            break

        a[i].pop()
        a[j].pop()
        tpairs+=1
    
    if tpairs==0:
        print(-1)
        break

    pairs-=tpairs
else:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n=int(input())
a=[0]+[list(map(int,input().split()[::-1])) for _ in range(n)]

pairs=n*(n-1)//2
ans=0

while pairs>0:
    ans+=1
    tpairs=0
    x=[]

    for i in range(1,n+1):
        if len(a[i])==0:
            continue
        x.append((min(i,a[i][-1]),max(i,a[i][-1])))
    
    cnt=Counter(x)

    for (i,j),v in cnt.items():
        if v==1:
            continue

        a[i].pop()
        a[j].pop()
        tpairs+=1
    
    if tpairs==0:
        print(-1)
        break

    pairs-=tpairs
else:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

n=int(input())
a=[0]+[list(map(int,input().split()[::-1])) for _ in range(n)]

pairs=n*(n-1)//2
ans=0

while pairs>0:
    ans+=1
    tpairs=0
    x=[]

    for i in range(1,n+1):
        if len(a[i])==0:
            continue
        x.append((min(i,a[i][-1]),max(i,a[i][-1])))
    
    cnt=Counter(x)

    for (i,j),v in cnt.items():
        if v==1:
            continue

        a[i].pop()
        a[j].pop()
        tpairs+=1
    
    if tpairs==0:
        print(-1)
        break

    pairs-=tpairs
else:
    print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n=int(input())
g=[set() for _ in range(n*(n-1)//2+1)]
visit=[0]*(n*(n-1)//2+1)
d=[0]*(n*(n-1)//2+1)

for i in range(1,n+1):
    last=0
    for j in list(map(int,input().split())):
        mi,ma=(min(i-1,j-1),max(i-1,j-1))
        current=ma*(ma-1)//2+mi+1
        g[last].add(current)
        last=current

def dfs(u):
    if visit[u]==2:
        return d[u]
    
    ans=0
    visit[u]=1
    for v in g[u]:
        if visit[v]==1:
            print(-1)
            exit(0)
        
        d[u]=max(d[u],1+dfs(v))
    
    visit[u]=2
    return d[u]

print(dfs(0))