Problem:
Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

4
2 3 4
1 3 4
4 1 2
3 1 2

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

struct topological_sort{
    vector<vector<int>> G;
    vector<int> indegree;
    int V;

    topological_sort(int V) : V(V) {G.resize(V); indegree.resize(V);}
    void add_edge(int a, int b) {
        G[a].push_back(b);
        indegree[b]++;
    }
    vector<int> sort() {
        queue<int> q;
        vector<int> sorted;
        for (int i = 0; i < V; i++) if (indegree[i] == 0) q.push(i);
        while (!q.empty()) {
            int v = q.front(); q.pop();
            for (int i = 0; i < G[v].size(); i++) {
                int u = G[v][i];
                indegree[u]--;
                if (indegree[u] == 0) q.push(u);
            }
            sorted.push_back(v);
        }
        return sorted;
    }
};

P change(P x) {
    if (x.first > x.second) swap(x.first,x.second);
    return x;
}

int main() {
    int n; cin >> n;
    topological_sort ts(n*(n-1)/2);
    map<P,int> to;
    map<int,P> from;
    int num = 0;
    rep(i,n)rep(j,i) {
        to[P(j,i)] = num;
        from[num] = P(j,i);
        num++;
    }
    vvint data(n,vint(n-1));
    rep(i,n)rep(j,n-1) {
        int x; cin >> x;
        x--;
        data[i][j] = x;
    }
    rep(i,n)rep(j,n-2) {
        P p1 = change(P(i,data[i][j]));
        P p2 = change(P(i,data[i][j+1]));
        ts.add_edge(to[p2],to[p1]);
    }
    vint res = ts.sort();
    if (res.size() != n*(n-1)/2) {cout << -1 << endl; return 0;}
    vint dp(n*(n-1)/2,1);
    rep(i,n*(n-1)/2) {
        P p = from[res[i]];
        rep(j,n-2) {
            if (data[p.first][j] == p.second) {
                P p1 = P(p.first,data[p.first][j+1]);
                p1 = change(p1);
                chmax(dp[to[p]],dp[to[p1]]+1);
            }
        }
        rep(j,n-2) {
            if (data[p.second][j] == p.first) {
                P p1 = P(p.second,data[p.second][j+1]);
                p1 = change(p1);
                chmax(dp[to[p]],dp[to[p1]]+1);
            }
        }
    }
    cout << dp[res.back()] << endl;
}