#include <bits/stdc++.h>
     using namespace  std;
    #include <ext/pb_ds/assoc_container.hpp>
    #include <ext/pb_ds/tree_policy.hpp>
    // What follows is a magical ordered_set data structure.
    // Supports the following functions:
    // find_by_order(ll k) - returns k'th smallest element (0-indexed)
    // order_of_key(ll k) - returns number of elements strictly smaller than k.
    using namespace __gnu_pbds;
    #define ordered_set tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> 
     
    #define mp make_pair
    #define pb push_back
    #define ff first
    #define ss second
    // #define ll long long
    const double pi=3.1415926535897932384626433832;
     long long mod= 1e9+7;
    //long long primeset[5000000]={};
    typedef complex<double> cd;
    typedef long long ll;
    typedef double ld;
    //#define For(i,0,n) for(long long i = 0 ; i<n ;i++) 
    //cd god (mod,0);
     
    cd fast_exp(cd a, long long b)
    {
    	if (b == 0)
    		return 1;
    	
    	cd res = fast_exp(a, b / 2);
    		
    	res = res *res;
    	
    	if (b % 2 == 1)
    		{
    			res = res * a;
    		 
     
    		}    
    	
    	return res;
    }
     
    long long fastexpon(long long a,long long b,long long mod)
    {
    	if(b==0)
    		return 1LL;
    	
    	long long res=fastexpon(a,b/2,mod);
    	
    	res=(res*res)%mod;
    	if(b%2==1)
    		res= (res * a)%mod;
     
    	return res;
    }
     
     
    //TEMPLATE ENDS HERE!!!! ------------------------------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------------------------------------------------------
     
     
struct custom
{
    bool operator()(ll a, ll b)
    {
		return a > b;
    }
};



int main()
{
     
    	// ios_base::sync_with_stdio(false);
    	// cin.tie(NULL);
    	// cout.tie(NULL);
        
        int n;
        cin>>n;
        int a[n+1][n-1];
        for(int i = 0; i<n ;i++)
        {
            for(int j = 0; j<n-1 ; j++)
            {
                cin>>a[i+1][j];
            }
        }
        int ptr[n+1]={};
        int countday = 0 , totcount = 0;
        int curcount = 0;
        int d[n+1]={};
     while(1)   
     {  
        curcount = 0;
        for(int i = 1 ; i<=n ;i++)
            d[i]=0;
        for(int i = 1 ; i<=n ;i++)
        {
            if(ptr[i]>=n-1)
                continue;
            int oth = a[i][ptr[i]];
            if(a[oth][ptr[oth]] == i && !d[i] && !d[oth])
            {
                curcount++;
                ptr[i]++;
                ptr[oth]++;
                d[i]++;
                d[oth]++;
            }
        }
        if(curcount)
        {
            countday++;
            totcount+=curcount;
        }
        else
            break;
    }

    if(totcount == n*(n-1)/2)
        cout<<countday<<endl;
    else
        cout<<-1<<endl;


        return 0;
     
}

Measured memory_bytes: 4096