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 end = false;

void dfs(long long v, long long len) {
	if (end) return;
	used[v] = 1;
	for (long long i = 0; i < with[v].size(); i++) {
		if (end) return;
		if (used[with[v][i]] == 1) {
			res = -1;
			end = 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 'end' is ambiguous
   55 |         if (end) return;
      |             ^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:166,
                 from a.cc:1:
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
In file included from /usr/include/c++/14/bits/algorithmfwd.h:39,
                 from /usr/include/c++/14/bits/stl_algo.h:59,
                 from /usr/include/c++/14/algorithm:61,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:52:6: note:                 'bool end'
   52 | bool end = false;
      |      ^~~
a.cc:58:21: error: reference to 'end' is ambiguous
   58 |                 if (end) return;
      |                     ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:52:6: note:                 'bool end'
   52 | bool end = false;
      |      ^~~
a.cc:61:25: error: reference to 'end' is ambiguous
   61 |                         end = 1;
      |                         ^~~
/usr/include/c++/14/valarray:1265:5: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
 1265 |     end(const valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/valarray:1249:5: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
 1249 |     end(valarray<_Tp>& __va) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:52:6: note:                 'bool end'
   52 | bool end = false;
      |      ^~~