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 a = new int[n];

    int match = 0;
    bool update = true;
    int date = 0;
    auto used = new bool[n];
    while (update) {
        used[] = false;
        update = false;
        for (int i = 0; i < n; i++) {
            // i 番目の対戦相手
            int index = a[i];
            if (index < n-1) {
                int t = g[i][index]; // 相手

                int tIndex = a[t];
                if (tIndex < n) {
                    int revT = g[t][tIndex];
                    if (revT == i && i < t && !used[i] && !used[t]) {
                        // writeln("match ", i, " ", t);
                        used[i] = used[t] = true;
                        a[i]++;
                        a[t]++;
                        match++;
                        update = true;
                    }
                }
            }
        }
        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));
}
