import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue = BinaryHeap!(Array!long, "a<b");
// dfmt on

/// Number of k-combinations
T combination(T = long)(T n, T k)
{
    assert(0 <= k);
    assert(0 <= n);
    if (n < k)
        return 0;
    k = min(n - k, k);
    if (k == 0)
        return 1;
    if (k == 1)
        return n;
    return memoize!combination(n - 1, k - 1) + memoize!combination(n - 1, k);
}

void main()
{
    long N = lread();
    long[][] A;
    foreach (_; 0 .. N)
    {
        A ~= aryread().map!"a-1"().array();
    }
    auto ptr = new long[N];
    long ans;
    long cnt;
    auto used = new bool[](N);
    while (true)
    {
        bool b = false;
        used[] = false;
        inner: foreach (i; 0 .. N)
        {

            if (N - 1 <= ptr[i] || used[i])
            {
                continue inner;
            }
            long j = A[i][ptr[i]];
            if (N - 1 <= ptr[j] || used[j])
            {
                continue inner;
            }
            if (A[j][ptr[j]] == i)
            {
                ptr[i]++;
                ptr[j]++;
                used[i] = used[j] = true;
                b = true;
                cnt++;
            }
        }
        if (!b)
        {
            writeln(-1);
            return;
        }
        ans++;
        if (N * (N - 1) / 2 <= cnt)
        {
            break;
        }
    }
    writeln(ans);
}
