Problem:
Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

4
2 3 4
1 3 4
4 1 2
3 1 2

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<cstdint>
#include<bits/stdc++.h>
#include<queue>
#include<set>
 
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for(int i = n-1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
 
#define INF bit(60)
#define pb push_back
#define mod 1000000007
 
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
 
using namespace std;
using uif=uint_fast64_t;
using ll=long long int;
 
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mod;a=1ll*a*a%mod;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
 
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
 
 
#define FACSIZE 2002
 
ll invfac[FACSIZE];
ll fac[FACSIZE];
 
ll mod_pow(ll ,ll );
ll mod_add(ll ,ll );
ll mod_mul(ll ,ll );
ll mod_div(ll ,ll );
void make_fact(ll );
void make_invfact(ll );
 
pair<ll,ll> dec( ll x ){
 
  ll a=x/10000;
  ll b=x%10000;
 
  return  make_pair( a,b );
 
}
 
ll enc( ll x, ll y ){
 
  //return p.first * 10000 + p.second;
  return x*10000 + y;
 
}

unordered_map< ll,ll > um_depth;
unordered_map< ll , vector<ll> > um_in;
unordered_map< ll , vector<ll> > um_out;

unordered_map< ll,ll > um_stat;

ll ans=0;
ll cnt=0;

bool dfs(ll x,ll d){  
  um_stat[x]=1;//checking
  cnt++;
  auto p=dec(x);
  //cout << p.first << "," << p.second << endl;
  um_depth[x]=d;
  ans=max(ans,d);

  for(auto v:um_out[x]){

    //if( um_stat[v]==1 ) return false;
    //else if( um_stat.find(v)==end(um_stat) ) if( !dfs(v,d+1) ) return false;
    if( um_stat.find(v)!=end(um_stat) ){
      if( um_stat[v]==1 ) return false;
    }
    else{
      if( !dfs(v,d+1) ) return false;
    }

  }

  um_stat[x]=2;//checked
  return true;
}

int main(void){
 
  ll n;
  cin>>n;
 
  vector< vector<ll> > A( n,vector<ll>(n-1) );
 
  using pa=pair<ll,ll>;
 
  queue< ll > no_in;
 
  //unordered_map<ll,ll> play;
  //vector< vector<ll> > play(n+1,vector<ll>(n+1,0));
 
  for(ll i=0;i<n;i++){
    for(ll j=0;j<n-1;j++){
      cin>>A[i][j];
      if(j == 0) continue;
      ll a,b;
      a=min(i+1, A[i][j] );
      b=max(i+1, A[i][j] );
      
      ll a_bf,b_bf;
      a_bf=min(i+1, A[i][j-1] );
      b_bf=max(i+1, A[i][j-1] );
      //cout << a << "," << b << "," << a_bf << "," << b_bf << endl;
      um_in[ enc(a,b) ].push_back( enc(a_bf,b_bf) );      
      um_out[ enc(a_bf,b_bf) ].push_back( enc(a,b) );

    }
  }

  for(ll i=1;i<n;i++){
    for(ll j=i+1;j<=n;j++){
      if( um_in.find( enc( i,j ) ) ==end(um_in)) no_in.push( enc(i,j) );
    }
  }

  while(!no_in.empty()){
    auto x=no_in.front();
    no_in.pop();
    if(!dfs(x,1)){
      cout << -1 << endl;
      return 0;
    }
  }
  
  //cout << cnt << endl;
  //cout << n*(n-1)/2 << endl;
  if(cnt < n*(n-1)/2 ){
    cout << -1 << endl;
    return 0;
  }

  cout << ans << endl;
  
  return 0;
 
}
 
ll mod_pow(ll x,ll r){
  if(r==0) return 1;
  else if(r==1) return x%mod;
  else if(r%2==0){
    ll t=mod_pow(x,r/2)%mod;
    return t*t %mod;
  } 
  else{
    ll t=mod_pow(x,r/2)%mod;
    ll k= t*t %mod;
    return k*x%mod;
  } 
}
ll mod_add(ll a,ll b){
  return (a+b)%mod;
}
 
ll mod_mul(ll a,ll b){
  return (a*b)%mod;
}
 
ll mod_div(ll a,ll b){
  return mod_mul(a,mod_pow(b,mod-2));
}
 
void make_fact(ll n){
  fac[0]=1;
  rep(i,n){
    fac[i+1]=mod_mul(fac[i],i+1);
  }  
}
 
void make_invfact(ll n){
  invfac[n]= mod_pow( fac[n] ,mod-2);
  for(int i=n-1;i>=0;i--){
    invfac[i]=mod_mul(invfac[i+1],i+1);
  }
 
}