Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(x):
        f = 1
        res = 0
        for y in v[x]:
            if d[y]:
                d[y] = 0
                f &= dfs(y)
                d[y] = 1
            else:
                return 0
            if not f:
                return 0
            if res < r[y]:
                res = r[y]
        r[x] = res+1
        return f

    n = I()
    a = LIR(n)
    N = n*(n-1)
    f = [0]*N
    v = [[] for i in range(N)]
    lis = set()
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
        k = a[i][0]
        x = n*min(i,k)+max(i,k)
        for j in range(1,n-1):
            l = a[i][j]
            y = n*min(i,l)+max(i,l)
            v[y].append(x)
            f[x] += 1
            lis.add(x)
            lis.add(y)
            x = y
    if min([f[i] for i in lis]):
        print(-1)
        return
    d = [1]*N
    r = [0]*N
    for i in lis:
        if not f[i]:
            d[i] = 0
            if not dfs(i):
                print(-1)
                return
    print(max([r[i] for i in lis]))
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    def dfs(x,p):
        if dp[x] != None:
            return dp[x]
        dp[x] = -1
        for y in v[x]:
            if p[y]:
                dp[x] = float("inf")
                break
            p[y] = 1
            dp[x] = max(dp[x], dfs(y,p)+1)
            p[y] = 0
        return dp[x]

    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    m = n*(n-1)
    v = [[] for i in range(m)]
    for i in range(n):
        p = n*min(i, a[i][0]) + max(i, a[i][0])
        for j in range(n-2):
            p_ = n*min(i, a[i][j+1]) + max(i, a[i][j+1])
            v[p_].append(p)
            p = p_
    dp = defaultdict(lambda : None)
    for i in range(m):
        if not v[i]:
            dp[i] = 1
    ans = 1
    p = [0]*m
    for x in range(m):
        p[x] = 1
        if ans < dfs(x,p):
            ans = dp[x]
        p[x] = 0
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()

Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(x):
        f = 1
        res = 0
        for y in v[x]:
            if d[y]:
                d[y] = 0
                f &= dfs(y)
                d[y] = 1
            else:
                return 0
            if not f:
                return 0
            if res < r[y]:
                res = r[y]
        r[x] = res+1
        return f

    n = I()
    a = LIR(n)
    N = n*(n-1)
    f = [0]*N
    v = [[] for i in range(N)]
    lis = set()
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
        k = a[i][0]
        x = n*min(i,k)+max(i,k)
        for j in range(1,n-1):
            l = a[i][j]
            y = n*min(i,l)+max(i,l)
            v[y].append(x)
            f[x] += 1
            lis.add(x)
            lis.add(y)
            x = y
    if min([f[i] for i in lis]):
        print(-1)
        return
    d = [1]*N
    r = [0]*N
    for i in lis:
        if not f[i]:
            d[i] = 0
            if not dfs(i):
                print(-1)
                return
    print(max([r[i] for i in lis]))
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    def dfs(x,p):
        if dp[x] != None:
            return dp[x]
        dp[x] = -1
        for y in v[x]:
            if p[y]:
                dp[x] = float("inf")
                break
            p[y] = 1
            dp[x] = max(dp[x], dfs(y,p)+1)
            p[y] = 0
        return dp[x]

    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    m = n*(n-1)
    v = [[] for i in range(m)]
    for i in range(n):
        p = n*min(i, a[i][0]) + max(i, a[i][0])
        for j in range(n-2):
            p_ = n*min(i, a[i][j+1]) + max(i, a[i][j+1])
            v[p_].append(p)
            p = p_
    dp = defaultdict(lambda : None)
    for i in range(m):
        if not v[i]:
            dp[i] = 1
    ans = 1
    p = [0]*m
    for x in range(m):
        p[x] = 1
        if ans < dfs(x,p):
            ans = dp[x]
        p[x] = 0
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()

Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(x):
        f = 1
        for y in v[x]:
            if d[y]:
                d[y] = 0
                f &= dfs(y)
                d[y] = 1
            else:
                return 0
            if not f:
                return f
        return f

    def dfs2(x):
        res = 0
        for y in v[x]:
            if d[y]:
                d[y] = 0
                dfs2(y)
            if res < r[y]:
                res = r[y]
        r[x] = res+1
    n = I()
    a = LIR(n)
    N = n*(n-1)
    f = [0]*N
    v = [[] for i in range(N)]
    lis = set()
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
        for j in range(n-2):
            k,l = a[i][j],a[i][j+1]
            x = n*min(i,k)+max(i,k)
            y = n*min(i,l)+max(i,l)
            v[y].append(x)
            f[x] += 1
            lis.add(x)
            lis.add(y)
    if min([f[i] for i in lis]):
        print(-1)
        return
    d = [1]*N
    for i in lis:
        if not f[i]:
            d[i] = 0
            if not dfs(i):
                print(-1)
                return
    r = [0]*N
    d = [1]*N
    for i in lis:
        if not f[i]:
            d[i] = 0
            dfs2(i)
    print(max([r[i] for i in lis]))
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    def dfs(x,p):
        if dp[x] != None:
            return dp[x]
        dp[x] = -1
        for y in v[x]:
            if p[y]:
                dp[x] = float("inf")
                break
            p[y] = 1
            dp[x] = max(dp[x], dfs(y,p)+1)
            p[y] = 0
        return dp[x]

    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    m = n*(n-1)
    v = [[] for i in range(m)]
    for i in range(n):
        p = n*min(i, a[i][0]) + max(i, a[i][0])
        for j in range(n-2):
            p_ = n*min(i, a[i][j+1]) + max(i, a[i][j+1])
            v[p_].append(p)
            p = p_
    dp = defaultdict(lambda : None)
    for i in range(m):
        if not v[i]:
            dp[i] = 1
    ans = 1
    p = [0]*m
    for x in range(m):
        p[x] = 1
        if ans < dfs(x,p):
            ans = dp[x]
        p[x] = 0
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()

Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(x):
        res = 0
        for y in v[x]:
            if d[y]:
                d[y] = 0
                dfs(y)
                if res < r[y]:
                    res = r[y]
                d[y] = 1
            else:
                r[x] = float("inf")
                f[x] = float("inf")
        r[x] = res + 1

    n = I()
    a = LIR(n)
    f = defaultdict(lambda : 0)
    v = defaultdict(lambda : [])
    lis = set()
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
        for j in range(n-2):
            k,l = a[i][j],a[i][j+1]
            x = (min(i,k),max(i,k))
            y = (min(i,l),max(i,l))
            v[y].append(x)
            f[x] += 1
            lis.add(x)
            lis.add(y)
    d = defaultdict(lambda : 1)
    q = deque([])
    r = defaultdict(lambda : 0)
    for i in lis:
        if not f[i]:
            d[i] = 0
            dfs(i)
    if max(f.values()) == float("inf") or min(f.values()):
        print(-1)
    else:
        print(max(r.values()))
    return

#Solve
if __name__ == "__main__":
    solve()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    def dfs(x,p):
        if dp[x] != None:
            return dp[x]
        dp[x] = -1
        for y in v[x]:
            if p[y]:
                dp[x] = float("inf")
                break
            p[y] = 1
            dp[x] = max(dp[x], dfs(y,p)+1)
            p[y] = 0
        return dp[x]

    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    m = n*(n-1)
    v = [[] for i in range(m)]
    for i in range(n):
        p = n*min(i, a[i][0]) + max(i, a[i][0])
        for j in range(n-2):
            p_ = n*min(i, a[i][j+1]) + max(i, a[i][j+1])
            v[p_].append(p)
            p = p_
    dp = defaultdict(lambda : None)
    for i in range(m):
        if not v[i]:
            dp[i] = 1
    ans = 1
    p = [0]*m
    for x in range(m):
        p[x] = 1
        if ans < dfs(x,p):
            ans = dp[x]
        p[x] = 0
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()

Buggy python submission (verdict: Time Limit Exceeded):
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    index = [0]*n
    ma = n*(n-1)
    m = ma>>1
    for t in range(m):
        k = [1 if index[i] < n-1 else 0 for i in range(n)]
        for i in range(n):
            if k[i]:
                j = a[i][index[i]]
                if k[j]:
                    if a[j][index[j]] == i:
                        index[i] += 1
                        index[j] += 1
                        k[i] = 0
                        k[j] = 0
        if sum(index) == ma:
            print(t+1)
            return
    print(-1)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()


Fixed submission:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    t = S()
    ans = 0
    for i in range(len(s)):
        if s[i] == t[i]:
            ans += 1
    print(ans)
    return

#B
def B():
    a,b = LI()
    for i in range(10000):
        if 1+i*a-i >= b:
            print(i)
            return
    return

#C
def C():
    n = I()
    a = LI()
    ans = 0
    r = 1
    for l in range(n):
        if r == l:
            r += 1
        while r < n and a[r-1] >= a[r]:
            r += 1
        ans = max(ans, r-l-1)
    print(ans)
    return

#D
def D():
    n = I()
    print(n*(n-1)>>1)
    return

#E
def E():
    def dfs(x,p):
        if dp[x] != None:
            return dp[x]
        dp[x] = -1
        for y in v[x]:
            if p[y]:
                dp[x] = float("inf")
                break
            p[y] = 1
            dp[x] = max(dp[x], dfs(y,p)+1)
            p[y] = 0
        return dp[x]

    n = I()
    a = LIR(n)
    for i in range(n):
        for j in range(n-1):
            a[i][j] -= 1
    m = n*(n-1)
    v = [[] for i in range(m)]
    for i in range(n):
        p = n*min(i, a[i][0]) + max(i, a[i][0])
        for j in range(n-2):
            p_ = n*min(i, a[i][j+1]) + max(i, a[i][j+1])
            v[p_].append(p)
            p = p_
    dp = defaultdict(lambda : None)
    for i in range(m):
        if not v[i]:
            dp[i] = 1
    ans = 1
    p = [0]*m
    for x in range(m):
        p[x] = 1
        if ans < dfs(x,p):
            ans = dp[x]
        p[x] = 0
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)
    return

#F
def F():
    n = I()
    p = LIR(n)

    return

#Solve
if __name__ == "__main__":
    E()