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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <algorithm>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
	for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " ");
	return os;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
	os << "(" << p.first << "," << p.second << ")";
	return os;
}

template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
	for (const auto &e : mp) os << e << " ";
	return os;
}

#ifdef _DEBUG
template <class T>
void dump(const char* str, T &&h) { cerr << str << " = " << h << "\n"; };
template <class Head, class... Tail>
void dump(const char* str, Head &&h, Tail &&... t) {
	while (*str != ',') cerr << *str++; cerr << " = " << h << "\n";
	dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else 
#define DMP(...) ((void)0)
#endif

class topological_sort {
private:
	int V;
	vector<vector<int>> edges;
	vector<int> in;
public:
	topological_sort(int n) :V(n), edges(n), in(n) {}
	topological_sort(vector<vector<int>> &edges) :V(edges.size()), in(edges.size()) { this->edges = edges; }


	void add_edge(int from, int to) { 
		DMP(from, to);
		edges[from].emplace_back(to); 
	}

	vector<int> build() {
		DMP(edges);
		for (int i = 0; i < V; i++) for (const auto &e : edges[i]) in[e]++;
		vector<int> used(V), res;
		res.reserve(V);

		while (true) {

			queue<int> que;

			for (int i = 0; i < V; i++) {
				if (in[i] == 0 && !used[i]) {
					used[i] = 1;
					que.emplace(i);
				}
			}

			if (que.empty()) break;

			while (!que.empty()) {
				int now = que.front();
				DMP(now, in);
				que.pop();
				res.emplace_back(now);
				for (const auto &e : edges[now]) {
					in[e]--;
					if (in[e] == 0) {
						if (used[e]) return vector<int>(); // unable to sort
						used[e] = 1;
						que.emplace(e);
					}
				}
			}
		}

		return res;
	}

};

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	map<pair<int, int>, int> id;
	vector<pair<int, int>> rid(N * (N - 1) / 2);

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			auto p = make_pair(i, j);
			if (id.find(p) == id.end()) {
				id[p] = cnt;
				rid[cnt] = p;
				cnt++;
			}
		}
	}
	DMP(rid, id);

	topological_sort ts(N * (N - 1) / 2);
	for (int i = 0; i < N; i++) {
		vector<int> A(N - 1);
		for (int j = 0; j < N - 1; j++) cin >> A[j];
		for (int j = 0; j < N - 2; j++) ts.add_edge(id[minmax(i, A[j] - 1)], id[minmax(i, A[j + 1] - 1)]);
	}

	auto ord = ts.build();
	DMP(ord);

	if (ord.empty()) cout << -1 << "\n";
	else {

		vector<int> used(N);
		int ans = 1;
		for (int i = 0; i < ord.size(); i++) {
			int row, col;
			tie(row, col) = rid[ord[i]];

			if (row == -1) continue;

			if (used[row] || used[col]) {
				ans++;
				fill(used.begin(), used.end(), 0);
			}
			
			used[row] = used[col] = 1;

		}

		cout << ans << "\n";

	}

	return 0;
}