Language: C++

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define rrep(i, a, n) for (int i = (int)(a); i >= (int)(n); --i)
#define debug(x) cerr << #x << " = " << x << "\n"
#define debugv(x) \
  rep(f, 0, (x.size() - 1)) cerr << x[f] << (f == (x.size() - 1) ? "\n" : " ")
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int INF = ((1LL << 30) + (1LL << 29));
const int MOD = 1000000007;
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;
}

int N, M, K;
string S, T;
vector<int> V;
int bt[1001][1001];
void input() {
  cin>>N;
  int k;
  rep(i,0,N-1){
    rep(j,0,N-2){
      cin>>k;
      k--;
      bt[i][j] = k; // 0-index
    }
  }

}

int days[1000*1000][1001];
//vector<priority_queue<int, vector<int>, greater<int>>> vq(1001);
vector<queue<int>> vq(1001);

void solve() {

  rep(i,0,N-1){
    rep(j,0,N-2){
      vq[i].push(bt[i][j]);
    }
  }

  bool findans=false;
  int ans=-1;

  set<int> battled;
  rep(j,1,1000*1000){
   bool allempty=true;
   battled = set<int>();
 //  debug(j);
    rep(i,0,N-1){
      if (not vq[i].empty())allempty=false;
      if (not vq[i].empty() && battled.find(i)==battled.end()){
        int t = vq[i].front(); // 相手
//        cerr<<i+1<<" vs "<<t+1<<endl;
        if (vq[t].empty() || (vq[t].front() != i)){
          // debug(t);
          // debug(vq[t].front());
          // debug(i);
          cout<<-1<<endl;
          return;
        }
        else {
          vq[i].pop();
          battled.insert(i);
          battled.insert(t);
          vq[t].pop();
        }
      }
    }
  //  debug(allempty);
    if (allempty) {
      ans=j;
      findans=true;
      break;
    }
  }

  //debug(findans);
  cout<<ans<<endl;




}
signed main() {
  input();
  solve();
  return 0;
}

Compiler output:
/tmp/ccBaUk3a.o: in function `solve()':
a.cc:(.text+0xd6): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x1c3): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x1ef): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x272): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x29d): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x2c2): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x332): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x385): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
/tmp/ccBaUk3a.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x51a): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
a.cc:(.text+0x540): relocation truncated to fit: R_X86_64_PC32 against symbol `vq' defined in .bss section in /tmp/ccBaUk3a.o
collect2: error: ld returned 1 exit status