stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}



	int k[1001] = {}, k2[1001] = {};

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = A[v][k[v]];

				if (A[a][k[a]] == v) {

					kari.pb(v);

					k2[v] = kotae;

					kari.pb(a);

					k2[a] = kotae;

				}

			}

		}

		V = kari;

		for (auto v : V) k[v]++;

	}



	int OK = 1;

	rep1(i, N) if (k[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.9955272851548926x):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	int* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}

	int* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					kari.pb(v);

					k[v]++;

					k2[v] = kotae;

					kari.pb(a);

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		swap(V, kari);

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.9539135014066433x):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	int* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}



	int k[1001] = {}, k2[1001] = {};

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = A[v][k[v]];

				if (A[a][k[a]] == v) {

					kari.pb(v);

					k2[v] = kotae;

					kari.pb(a);

					k2[a] = kotae;

				}

			}

		}

		swap(V, kari);

		for (auto v : V) k[v]++;

	}



	int OK = 1;

	rep1(i, N) if (k[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.724338871142565x):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	short A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	short* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}



	int k[1001] = {}, k2[1001] = {};

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = A[v][k[v]];

				if (A[a][k[a]] == v) {

					kari.pb(v);

					k2[v] = kotae;

					kari.pb(a);

					k2[a] = kotae;

				}

			}

		}

		V = kari;

		for (auto v : V) k[v]++;

	}



	int OK = 1;

	rep1(i, N) if (k[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.758028145124025x):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	short A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	short* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}

	int* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					kari.pb(v);

					k[v]++;

					k2[v] = kotae;

					kari.pb(a);

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		swap(V, kari);

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.708472844867428x):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	short A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	short* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<queue<ll>>a(1010);

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push(x);

		}

	}

	set<ll>ch;

	for(ll i=0;i<n;i++){

		ll nex=a[i].front();

		if(a[nex].front()==i){

			ch.insert(i);

			//cout << i<<endl;

		}

	}

	ll cnt=0;

	while(ch.size()!=0){

		cnt++;

		vector<ll>b;

		for(auto itr=ch.begin();itr!=ch.end();itr++){	

			a[*itr].pop();

			b.push_back(*itr);

		}

		ch.clear();

		while(b.size()!=0){

			ll now=b.back();

			b.pop_back();

			if(a[now].size()==0){

				continue;

			}

			ll nex=a[now].front();

			if(a[nex].front()==now){

				ch.insert(now);

				ch.insert(nex);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()!=0){

			cout << -1;

			return 0;

		}

	}

	cout <<cnt;

	// your code goes here

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2941322559294068x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<ll>a[1000];

	vector<ll>st;

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push_back(x);

		}

		reverse(a[i].begin(),a[i].end());

		st.push_back(i);

	}

	ll cnt=0;

	while(st.size()!=0){

		cnt++;

		bool ok[1000];

		for(ll i=0;i<n;i++){

			ok[i]=false;

		}

//		cout <<cnt<<endl;

		while(st.size()!=0){

			ll now=st.back();

			st.pop_back();

			ll next=a[now].back();

			if(ok[now]||ok[next]){

				continue;

			}

			if(a[next].back()==now){

				ok[now]=true;

				ok[next]=true;

				a[now].pop_back();

				a[next].pop_back();

			}

		}

		for(ll i=0;i<n;i++){

		//	cout << i <<ok[i]<<endl;

			if(ok[i]==true&&a[i].size()!=0){

				st.push_back(i);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()){

			cout <<-1;

			return 0;

		}

	}

	cout << cnt;

	// your code goes here

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<queue<ll>>a(1010);

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push(x);

		}

	}

	set<ll>ch;

	for(ll i=0;i<n;i++){

		ll nex=a[i].front();

		if(a[nex].front()==i){

			ch.insert(i);

			//cout << i<<endl;

		}

	}

	ll cnt=0;

	while(ch.size()!=0){

		cnt++;

		vector<ll>b;

		for(auto itr=ch.begin();itr!=ch.end();itr++){	

			a[*itr].pop();

			b.push_back(*itr);

		}

		ch.clear();

		while(b.size()!=0){

			ll now=b.back();

			b.pop_back();

			if(a[now].size()==0){

				continue;

			}

			ll nex=a[now].front();

			if(a[nex].front()==now){

				ch.insert(now);

				ch.insert(nex);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()!=0){

			cout << -1;

			return 0;

		}

	}

	cout <<cnt;

	// your code goes here

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.2925769396979119x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<ll>a[1000];

	vector<ll>st;

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push_back(x);

		}

		reverse(a[i].begin(),a[i].end());

		st.push_back(i);

	}

	ll cnt=0;

	while(st.size()!=0){

		cnt++;

		bool ok[1000]={false};

//		cout <<cnt<<endl;

		while(st.size()!=0){

			ll now=st.back();

			st.pop_back();

			ll next=a[now].back();

			if(ok[now]||ok[next]){

				continue;

			}

			if(a[next].back()==now){

				ok[now]=true;

				ok[next]=true;

				a[now].pop_back();

				a[next].pop_back();

			}

		}

		for(ll i=0;i<n;i++){

		//	cout << i <<ok[i]<<endl;

			if(ok[i]==true&&a[i].size()!=0){

				st.push_back(i);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()){

			cout <<-1;

			return 0;

		}

	}

	cout << cnt;

	// your code goes here

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



#define  ll long long

#define  il inline

#define INF 0x3f3f3f3f

#define pii pair<int,int>

#define rep(i,a,b) for(int i=(a);i<=(b);++i)

#define dep(i,a,b) for(int i=(a);i>=(b);--i)

#define pb push_back



const int mod = 1e9 + 7;

const int maxn = 1e3 + 5;

const int maxm = 2e5 + 10;

const double PI = acos(-1.0);



int n;

queue<int>a[maxn];

int day[maxn];



int main()

{

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

    cin >> n;

    rep(i,1,n) {

    	rep(j,1,n - 1) {

    		 int c; 

    		 cin >> c;

    		 a[i].push(c);

    	}

    }

    int sum = n * (n - 1);

    while(1){

    	bool flag = false;

    	rep(i,1,n){

    		if(a[i].empty()) continue;

    		if(a[a[i].front()].front() == i) {

    			day[i] = day[a[i].front()] = max(day[i],day[a[i].front()]) + 1;

    			//cout << i << " " << a[i].front() << endl;

    			a[a[i].front()].pop();

    			a[i].pop();

    			sum -= 2;

    			flag = true;

    		}

    	}

    	//cout << sum << endl;

    		if(flag == false) {

    			cout << -1 << endl;

    			return 0;

    		}

    		if(!sum) break;



    }

    int ans = 0;

    rep(i,1,n) ans = max(ans,day[i]);

    cout << ans << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.294201142975841x):
#include<bits/stdc++.h>

using namespace std;



#define  ll long long

#define  il inline

#define INF 0x3f3f3f3f

#define pii pair<int,int>

#define rep(i,a,b) for(int i=(a);i<=(b);++i)

#define dep(i,a,b) for(int i=(a);i>=(b);--i)

#define pb push_back



const int mod = 1e9 + 7;

const int maxn = 1e3 + 10;

const int maxm = 2e5 + 10;

const double PI = acos(-1.0);



struct Edge{

	int v,next;

}edge[maxn*maxn];

int head[maxn*maxn],tot=0;

int n,in[maxn*maxn];

int a[maxn][maxn];

int id[maxn][maxn];

int day[maxn*maxn];



void init(int n){

	tot=0;

	for(int i=1;i<=n;i++) head[i]=-1;

}

void add(int u,int v){

	edge[tot].v=v;

	edge[tot].next=head[u];

	head[u]=tot++;

}

void tuopu(int n)

{

	queue<int>Q;

	for(int i=1;i<=n;i++)

	{

		if(in[i]==0)

		{

			Q.push(i); 

			day[i] = 1;

			//cout << i << endl;

		}

	}

	while(!Q.empty())

	{

		int u=Q.front();

		Q.pop();

		for(int i=head[u];i!=-1;i=edge[i].next)

		{

			int v=edge[i].v;

			//cout << v << endl;

			in[v]--;

			day[v] = day[u] + 1;

			if(!in[v])

			{

				Q.push(v);

			}

		}

	}

}



int main()

{

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

   	cin >> n;

   	rep(i,1,n) rep(j,1,n-1) cin >> a[i][j];

   	int cnt = 0;

   	rep(i,1,n) rep(j,i + 1, n) id[i][j] = id[j][i] = ++cnt;

   	init(cnt);

   	rep(i,1,n) {

   		rep(j,2,n - 1) {

   			//cout << id[i][a[i][j - 1]] << " " << id[i][a[i][j]] << endl;

   			add(id[i][a[i][j - 1]],id[i][a[i][j]]);

   			in[id[i][a[i][j]]]++;

   		}

   	}

   	tuopu(cnt);

   	rep(i,1,cnt){

   		if(in[i]){

   			cout << -1 << endl;

   			return 0;

   		}

   	}

   	int ans = 0;

   	rep(i,1,cnt) ans = max(ans,day[i]);

   	cout << ans << endl;

    return 0;

}