### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
from collections import defaultdict
from itertools import product
INF = float("inf")


def main():
    N = int(input())
    A = [None]*N
    for i in range(N):
        A[i] = list(map(lambda x: int(x)-1, input().split()))

    def f(i, j):
        # 0<=i<j<Nを満たすi,jの組を整数へ対応付ける
        if i > j:
            i, j = j, i
        return j-1 + (N-2)*i-i*(i-1)//2

    out = defaultdict(list)
    inn = defaultdict(list)
    # 対戦カードをノードとみなし、閉路検出と最長パス探索を行う
    for i in range(N):
        for j in range(N-2):
            out[f(i, A[i][j])].append(f(i, A[i][j+1]))
            inn[f(i, A[i][j+1])].append(f(i, A[i][j]))
    # print(out)

    # 閉路検出
    # 始点からのパスを保持しながらDFSする
    hist = [False]*(N*(N-1)//2)

    def dfs(v):
        hist[v] = True
        longpath = 1
        longlast = v
        for u in out[v]:
            if hist[u] is False:
                curr, last = dfs(u)
                if 1+curr > longpath:
                    longpath = 1+curr
                    longlast = last
            else:
                return INF, -1
        hist[v] = False
        return longpath, longlast

    longest, last = dfs(f(0, A[0][0]))
    if longest == INF:
        print(-1)
        return

    def dfs2(v):
        hist[v] = True
        longpath = 1
        longlast = v
        for u in inn[v]:
            if hist[u] is False:
                curr, last = dfs2(u)
                if 1+curr > longpath:
                    longpath = 1+curr
                    longlast = last
            else:
                return INF, -1
        hist[v] = False
        return longpath, longlast

    longest, last = dfs2(last)
    if last == INF:
        print(-1)
        return
    else:
        print(longest)
    return


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
from collections import deque
import heapq
INF = float("inf")


def main():
    # 10**6くらいのループ
    # 毎回、logN以内で閉路を検出できると嬉しい
    # 見るべき場所は、新しく追加したところだけで、即座にわかるのでO(1)か
    N = int(input())
    A = [None]*N
    for i in range(N):
        A[i] = list(map(lambda x: int(x)-1, input().split()))
    # print(A)

    # それぞれの選手の手合進行
    indices = [0]*N
    E = [-1 for _ in range(N)]
    change = []
    for i in range(N):
        # 選手iは次選手A[i][indices[i]]と勝負したい
        E[i] = A[i][indices[i]]
        if E[E[i]] == i:
            # 勝負成立
            change.append(i)
            change.append(E[i])
            indices[i] += 1
            indices[E[i]] += 1
    if len(change) == 0:
        # print("一巡目終了")
        print(-1)
        return

    ans = 0
    while len(change) > 0:
        # print("change", change)
        # print("indices", indices)
        new_change = []
        ans += 1
        for c in change:
            # print("変更", c)
            if indices[c] == N-1:
                continue
            E[c] = A[c][indices[c]]
            if E[E[c]] == c:
                # 勝負成立
                # print("勝負成立, ", c, " vs ", E[c])
                # print(indices)
                new_change.append(c)
                new_change.append(E[c])
                indices[c] += 1
                indices[E[c]] += 1
        change = new_change

    flag = True
    for i in range(N):
        if indices[i] != N-1:
            flag = False
            break
    if flag is False:
        # print(indices)
        print(-1)
        return
    print(ans)

    return


if __name__ == '__main__':
    main()