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++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
#include <cassert>
#include <random>
#include <time.h>
//#include <boost/multiprecision/cpp_int.hpp>



using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const double EPS = 1e-10;
long long const MOD = 1000000007;

long long mod_pow(long long x, long long n) {
    long long res = 1;
    for (int i = 0;i < 60; i++) {
        if (n >> i & 1) res = res * x % MOD;
        x = x * x % MOD;
    }
    return res;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

template<typename T>
T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

void fastInput() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

int A[1005][1005];

stack<int> st[1005];

int main(void) {
    int N; cin >> N;
    for (int i = 1; i <= N; i++) {
        for (int j = 0; j < N-1; j++) {
            cin >> A[i][j];
        }
    }
    for (int i = 1; i <= N; i++) {
        for (int j = N-2; 0 <= j; j--) {
            st[i].push(A[i][j]);
        }
    }

    vector<int> sch;
    for (int i = 0; i < N-1; i++) {
        for (int j = 1; j <= N; j++) {
            if (st[j].empty()) continue;
            if (st[st[j].top()].empty()) continue;

            if (j == st[st[j].top()].top()) {
                int a = j;
                int b = st[j].top();
                sch.push_back(a);
                sch.push_back(b);
                st[st[j].top()].pop();
                st[j].pop();
            }
        }
    }

    // for (auto itr : sch) {
    //     cout << itr << endl;
    // }
    // cout << endl;
    set<int> s;
    Int ans = 1;
    if (sch.size() != N*(N-1)) {
        cout << -1 << endl;
        return 0;
    }

    for (int i = 1; i < sch.size(); i += 2) {
        int a = sch[i];
        int b = sch[i-1];
        if (s.count(a) || s.count(b)) {
            ans++;
            s.clear();
        }
        s.insert(a);
        s.insert(b);
    }
    cout << ans << endl;
}