Language: C++

#include<iostream>
#include<vector>
#include<set>
#include<functional>
#include<queue>
#include<limits>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

class Match {
	public:
		uint A, B;
		uint nv = 0;
		uint dist = 0;
	public:
		Match(uint a, uint b) {
			A = min(a, b);
			B = max(a, b);
		}
};

bool operator==(const Match& m1, const Match& m2) {
	return (m1.A == m2.A) & (m1.B == m2.B);
}

int main() {
	uint N;
	cin >> N;
	
	vector<vector<uint>> A(N, vector<uint>(N-1));
	vector<Match> matches;
	
	for(uint i=0; i<N; i++) {
		for(uint j=0; j<N-1; j++) {
			cin >> A[i][j];
			if (i+1 < A[i][j]){
				Match m = Match(i+1, A[i][j]);
				matches.push_back(m);
			}
		}
	}
	
	// Search closed + longest path
	queue<reference_wrapper<Match>> mq;
	uint loop = 0;
	uint dmx = 0;
	
	for(Match& m : matches) {
		m.dist = 1;
		mq.push(m);
		
		while(!mq.empty()) {
			Match& cur = mq.front();
			mq.pop();
			//cout << "cur : "<< cur.A << " "<<  cur.B << " "<< cur.nv << " "<< cur.dist << endl;
			
          	uint nA = cur.A;
			uint nB = cur.B;
			
			auto itrA = find(A[nA-1].begin(), A[nA-1].end(), nB);
			
			if(++itrA != A[nA-1].end()) {
              	
			    Match nm = Match(nA, *itrA);
			    auto itr = find(matches.begin(), matches.end(), nm);
			    //cout << "nxA : "<< itr->A << " "<<  itr->B << " "<< itr->nv << " "<< itr->dist << endl;
			
			    if (itr->nv > loop) {
					cout << -1;
					return 0;
				} else if (itr->nv < loop) {
					itr->dist = cur.dist + 1;
					mq.push(*itr);
				} else if (cur.dist + 1 > itr->dist) {
					itr->dist = cur.dist + 1;
					mq.push(*itr);
				}
			}
		
			auto itrB = find(A[nB-1].begin(), A[nB-1].end(), nA);
				
			if(++itrB != A[nB-1].end()) {
			
			    Match nm = Match(nB, *itrB);
			    auto itr = find(matches.begin(), matches.end(), nm);
			    //cout << "nxB : "<< itr->A << " "<<  itr->B << " "<< itr->nv << " "<< itr->dist << endl;
			
			    if (itr->nv > loop) {
					cout << -1;
					return 0;
				} else if (itr->nv < loop) {
					itr->dist = cur.dist + 1;
					mq.push(*itr);
				} else if (cur.dist + 1 > itr->dist) {
					itr->dist = cur.dist + 1;
					mq.push(*itr);
				}
			}
			
			cur.nv = loop + 1;
			dmx = max(dmx, cur.dist);
		}
		
		for(Match& nm : matches) {
			nm.dist = 0;
		}
		loop++;
	}
	
	cout << dmx << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:63:41: error: no matching function for call to 'find(std::vector<unsigned int>::iterator, std::vector<unsigned int>::iterator, uint&)'
   63 |                         auto itrA = find(A[nA-1].begin(), A[nA-1].end(), nB);
      |                                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/locale_facets.h:48,
                 from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:63:41: note:   '__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   63 |                         auto itrA = find(A[nA-1].begin(), A[nA-1].end(), nB);
      |                                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:68:44: error: no matching function for call to 'find(std::vector<Match>::iterator, std::vector<Match>::iterator, Match&)'
   68 |                             auto itr = find(matches.begin(), matches.end(), nm);
      |                                        ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:68:44: note:   '__gnu_cxx::__normal_iterator<Match*, std::vector<Match> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   68 |                             auto itr = find(matches.begin(), matches.end(), nm);
      |                                        ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:83:41: error: no matching function for call to 'find(std::vector<unsigned int>::iterator, std::vector<unsigned int>::iterator, uint&)'
   83 |                         auto itrB = find(A[nB-1].begin(), A[nB-1].end(), nA);
      |                                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:83:41: note:   '__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   83 |                         auto itrB = find(A[nB-1].begin(), A[nB-1].end(), nA);
      |                                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:88:44: error: no matching function for call to 'find(std::vector<Match>::iterator, std::vector<Match>::iterator, Match&)'
   88 |                             auto itr = find(matches.begin(), matches.end(), nm);
      |                                        ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:88:44: note:   '__gnu_cxx::__normal_iterator<Match*, std::vector<Match> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   88 |                             auto itr = find(matches.begin(), matches.end(), nm);
      |                                        ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~