import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;
    
    int[][] ord;
    foreach (_; 0 .. n) {
        ord ~= readln.chomp.split.map!(to!int).map!(x => x - 1).array;
    }
    
    /*
    auto g = new int[][] (n);
    foreach (i; 0 .. n) {
        ord[i].reverse();
        
        debug { ord[i].writeln; }
        
        g[i] ~= ord[i].back;
        ord[i].popBack();
    }
    
    debug { g.each!writeln; }
    */
    
    auto pos = new int[] (n);
    auto day = new int[] (n);
    auto vis = new bool[] (n);
    
    int getMatch(int v) { return v != -1 && pos[v] < n-1 ? ord[v][pos[v]] : -1; }
    
    bool dfs(int v) {
        if (vis[getMatch(v)]) {
            return false;
        }
        vis[v] = true;
        
        while (getMatch(getMatch(v)) == v) {
            int nxt = getMatch(v);
            day[v] = day[nxt] = max(day[v] + 1, day[nxt] + 1);
            pos[v] += 1;
            pos[nxt] += 1;
        }
        
        if (getMatch(v) != -1) {
            return dfs(getMatch(v));
        }
        
        return true;
    }
    
    foreach (i; 0 .. n) {
        while (getMatch(i) != -1) {
            bool ok = dfs(i);
            if (!ok) {
                writeln(-1);
                return;
            }
        }
    }
    
    int ans = 1;
    foreach (e; day) { ans = max(ans, e); }
    
    ans.writeln;
}