Language: C++

#include <bits/stdc++.h>

using namespace std;

vector < long long > with[1000006];
long long a[1003][1003];

long long n;

long long PairToInt(long long a, long long b) {
	if (a > b) swap(a, b);
	return (n * (a - 1) + (b - 1));
}

long long First(long long a) {
	return (a / n + 1);
}

long long Second(long long b) {
	return (b % n + 1);
}

long long res = 0;

long long used[1000006];

void bfs() {
	for (long long i = 0; i <= n * (n - 2) + n - 1; i++) {
		if (First(i) >= Second(i)) continue;
		cout<<First(i)<<" "<<Second(i)<<endl;
		if (used[i]) continue;
		used[i] = i;
		vector < pair < long long, long long > > que;
		que.push_back({i, 0});
		long long head = 0, tail = 0;
		for (; head <= tail; head++) {
			res = max(res, que[head].second);
			for (long long j = 0; j < with[que[head].first].size(); j++) {
				if (used[with[que[head].first][j]] == i) {
					cout<<"Exit with "<<que[head].first<<" "<<with[que[head].first][j]<<endl;
					cout<<"-1\n";
					exit(0);
				}
				que.push_back({with[que[head].first][j], que[head].second + 1});
				tail++;
				used[with[que[head].first][j]] = i;
			}
		}
	}
}

bool ends = false;

void dfs(long long v, long long len) {
	if (ends) return;
	used[v] = 1;
	for (long long i = 0; i < with[v].size(); i++) {
		if (ends) return;
		if (used[with[v][i]] == 1) {
			res = -1;
			ends = 1;
			return;
		}
		dfs(with[v][i], len + 1);
	}
	if (res >= 0) res = max(res, len);
	used[v] = 2; 
}

int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	cin>>n;
	for (long long i = 1; i <= n; i++) 
	for (long long j = 1; j < n; j++)
		cin>>a[i][j];
	for (long long i = 1; i <= n; i++)
	for (long long j = 2; j < n; j++)
		with[PairToInt(i, a[i][j - 1])].push_back(PairToInt(i, a[i][j]));
		
	/*for (long long i = 0; i <= n * (n - 2) + n - 1; i++) {
		cout<<i<<": ";
		for (long long j = 0; j < with[i].size(); j++)
			cout<<with[i][j]<<" ";
		cout<<endl;
	}*/
	
	//bfs();
	for (long long i = 0; i <= n * (n - 2) + n - 1; i++)
		if (First(i) < Second(i) && !used[i]) dfs(i, 0);
	if (res == -1) res --;
	cout << (res + 1) << endl;
}

Compiler output:
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:55:13: error: reference to 'ends' is ambiguous
   55 |         if (ends) return;
      |             ^~~~
In file included from /usr/include/c++/14/istream:41,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127,
                 from a.cc:1:
/usr/include/c++/14/ostream:756:5: note: candidates are: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::ends(basic_ostream<_CharT, _Traits>&)'
  756 |     ends(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:52:6: note:                 'bool ends'
   52 | bool ends = false;
      |      ^~~~
a.cc:58:21: error: reference to 'ends' is ambiguous
   58 |                 if (ends) return;
      |                     ^~~~
/usr/include/c++/14/ostream:756:5: note: candidates are: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::ends(basic_ostream<_CharT, _Traits>&)'
  756 |     ends(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:52:6: note:                 'bool ends'
   52 | bool ends = false;
      |      ^~~~
a.cc:61:25: error: reference to 'ends' is ambiguous
   61 |                         ends = 1;
      |                         ^~~~
/usr/include/c++/14/ostream:756:5: note: candidates are: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::ends(basic_ostream<_CharT, _Traits>&)'
  756 |     ends(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:52:6: note:                 'bool ends'
   52 | bool ends = false;
      |      ^~~~