Language: C++

/* monkukui 競技プログラミング用のテンプレート  (ここから) */
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
using ll = long long int;
using lnt = long long int;
using graph = vector<vector<long long int>>;
using wgraph = vector<vector<pair<long long int, long long int>>>;
long long int INF = 1001001001001001LL;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
long long int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
long long int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
inline void yes(){ cout << "yes" << endl; }
inline void Yes(){ cout << "Yes" << endl; }
inline void YES(){ cout << "YES" << endl; }
inline void no(){ cout << "no" << endl; }
inline void No(){ cout << "No" << endl; }
inline void NO(){ cout << "NO" << endl; }
inline void possible(){ cout << "possible" << endl; }
inline void Possible(){ cout << "Possible" << endl; }
inline void POSSIBLE(){ cout << "POSSIBLE" << endl; }
inline void impossible(){ cout << "impossible" << endl; }
inline void Impossible(){ cout << "Impossible" << endl; }
inline void IMPOSSIBLE(){ cout << "IMPOSSIBLE" << endl; }

#define rep(i,n) for(long long int i = 0; i < (n); i++)
#define rrep(i,n) for(long long int i = 1; i <= (n); i++)
#define drep(i,n) for(long long int i = (n)-1; i >= 0; i--)
#define srep(i,s,t) for(long long int i = s; i < t; i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back

/* monkukui 競技プログラミング用のテンプレート (ここまで)*/

int main(){
  
  int n; cin >> n;
  vector<vector<int>> a(n, vector<int> (n - 1));

  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n - 1; j++) {
      cin >> a[i][j];  
      a[i][j]--;
    }
  }

  vector<int> idxs(n, 0);
  int ans = 1;
  
  set<int> st;
  set<int> end;
  for(int i = 0; i < n; i++) st.insert(i); 
  
  while(1) {

    if(st.size() == 0){

      cout << -1 << endl;
      return 0;
    }
    set<int> nxtst;
    
    for(auto x : st) {
      
      int player = x;
      int pid = idxs[player];
      if(pid >= n - 1) continue;
      int oponent = a[player][pid];
      if(oid >= n - 1) continue;
      int oid = idxs[oponent];

      if(nxtst.find(player) != nxtst.end()) continue;
      if(nxtst.find(oponent) != nxtst.end()) continue;
      if(player == a[oponent][oid]){
        
        
        idxs[player]++;
        idxs[oponent]++;

        if(idxs[player] == n - 1) end.insert(player);
        if(idxs[oponent] == n - 1) end.insert(oponent);

        nxtst.insert(player);
        nxtst.insert(oponent);
      }
    }
  
    if(end.size() == n) break;
    ans++;

    st.clear();
    st = nxtst;
    nxtst.clear();
  }
  
  cout << ans << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:96:10: error: 'oid' was not declared in this scope; did you mean 'pid'?
   96 |       if(oid >= n - 1) continue;
      |          ^~~
      |          pid