import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int[][] g) {
    int n = cast(int)g.length;

    auto ind = new int[n];

    auto gg = new int[][](n, n);
    int[][] q;
    for (int i = 0; i < n; i++) {
        // i 番目の対戦相手
        int index = ind[i]++;
        if (index == n) continue;
        int t = g[i][index]; // 相手

        int a = min(i, t);
        int b = max(i, t);
        gg[a][b]++;
        if (gg[a][b] == 2) {
            q ~= [a, b];
        }
    }

    int match = 0;
    bool update = true;
    int date = 0;
    auto used = new bool[n];
    while (update) {
        used[] = false;
        update = false;
        int[][] qq;
        while (q.length > 0) {
            auto x = q[$-1]; q.popBack();
            int a = x[0];
            int b = x[1];
            if (!used[a] && !used[b]) {
                used[a] = used[b] = true;
                match++;
                // writeln("match ", a, " ", b);
                update = true;

                int aIndex = ind[a]++;
                if (aIndex < n-1) { // 次の対戦相手
                    int t = g[a][aIndex];
                    int mi = min(a, t);
                    int ma = max(a, t);
                    gg[mi][ma]++;
                    if (gg[mi][ma] == 2) {
                        qq ~= [mi, ma];
                    }
                }

                int bIndex = ind[b]++;
                if (bIndex < n-1) {
                    int t = g[b][bIndex];
                    int mi = min(b, t);
                    int ma = max(b, t);
                    gg[mi][ma]++;
                    if (gg[mi][ma] == 2) {
                        qq ~= [mi, ma];
                    }
                }
            }
        }

        for (int i = 0; i < qq.length; i++) q ~= qq[i];



        if (update) date++;
    }

    if (match == n * (n - 1) / 2) return date;
    return -1;
}

void main() {
    int n; scan(n);
    int[][] g;
    for (int i = 0; i < n; i++) {
        g ~= readints.map!(e => e - 1).array;
    }
    writeln(calc(g));
}
