Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
xss = [[int(x)-1 for x in input().split()] for _ in range(n)]

class Stack:
    def __init__(xs):
        self.__list = list(reversed(xs))

    def peak(self):
        return self.__list[-1]

    def pop(self):
        return self.pop()

def solve(n, xss):
    yss = [[-1] + list(reversed(xs)) for xs in xss]
    ans = 0
    while True:
        ms = list(matches(yss))
        if len(ms) == 0:
            if all(len(ys)==1 for ys in yss):
                return ans
            else:
                return -1
            return ans
        ans += 1
        for x, y in ms:
            yss[x].pop()
            yss[y].pop()

    for i in range(n):
        while len(yss[i]) > 0:
            match = find_first_match(yss, i)
            if match is None:
                return [-1]
            x, y = match
            ans

def matches(yss):
    firsts = [ys[-1] for ys in yss]
    for x, y in enumerate(firsts):
        if y!=-1 and x < y and firsts[y]==x:
            yield y, firsts[y]

print(solve(n, xss))


Fixed submission:
n = int(input())
xss = [[int(x)-1 for x in input().split()] for _ in range(n)]

def solve(n, xss):
    yss = [[-1] + list(reversed(xs)) for xs in xss]
    ans = 0
    candidates = range(n)
    while True:
        #print(" ".join(map(str, candidates)))
        ms = list(matches(yss, candidates))
        if len(ms) == 0:
            if all(len(ys)==1 for ys in yss):
                return ans
            else:
                return -1
        ans += 1
        candidates = []
        #print(" ".join(map(str, ms)))
        for x, y in ms:
            yss[x].pop()
            yss[y].pop()
            candidates.append(x)
            candidates.append(y)

def matches(yss, candidates):
    firsts = [yss[i][-1] for i in candidates]
    ans = set()
    for x, y in zip(candidates, firsts):
        if y!=-1 and yss[y][-1] ==x:
            ans.add((min(x, y), max(x, y)))
    return ans

print(solve(n, xss))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
xss = [[int(x)-1 for x in input().split()] for _ in range(n)]

class Stack:
    def __init__(xs):
        self.__list = list(reversed(xs))

    def peak(self):
        return self.__list[-1]

    def pop(self):
        return self.pop()

def solve(n, xss):
    yss = [[-1] + list(reversed(xs)) for xs in xss]
    ans = 0
    while True:
        ms = list(matches(yss))
        if len(ms) == 0:
            if all(len(ys)==1 for ys in yss):
                return ans
            else:
                return -1
            return ans
        ans += 1
        for x, y in ms:
            yss[x].pop()
            yss[y].pop()

    for i in range(n):
        while len(yss[i]) > 0:
            match = find_first_match(yss, i)
            if match is None:
                return [-1]
            x, y = match
            ans

def matches(yss):
    firsts = [ys[-1] for ys in yss]
    for x, y in enumerate(firsts):
        if y!=-1 and x < y and firsts[y]==x:
            yield y, firsts[y]

print(solve(n, xss))

Fixed submission:
n = int(input())
xss = [[int(x)-1 for x in input().split()] for _ in range(n)]

def solve(n, xss):
    yss = [[-1] + list(reversed(xs)) for xs in xss]
    ans = 0
    candidates = range(n)
    while True:
        #print(" ".join(map(str, candidates)))
        ms = list(matches(yss, candidates))
        if len(ms) == 0:
            if all(len(ys)==1 for ys in yss):
                return ans
            else:
                return -1
        ans += 1
        candidates = []
        #print(" ".join(map(str, ms)))
        for x, y in ms:
            yss[x].pop()
            yss[y].pop()
            candidates.append(x)
            candidates.append(y)

def matches(yss, candidates):
    firsts = [yss[i][-1] for i in candidates]
    ans = set()
    for x, y in zip(candidates, firsts):
        if y!=-1 and yss[y][-1] ==x:
            ans.add((min(x, y), max(x, y)))
    return ans

print(solve(n, xss))