#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;

int main(){
    int n;cin>>n;
    vector<queue<int>> tennis(n);
    int a;
    rep(i,n)rep(j,n-1){
        cin>>a;
        tennis[i].push(a-1);
    }
    vector<queue<int>> match(2);
    rep(i,n)match[0].push(i);
    int ans=0,cnt=0,tmp,res=0;;bool can;
    while(1){
        ans++;
        vbo already(n,false);
        can=false;
        while(match[res].size()){
            int i=match[res].front();match[res].pop();
            if(tennis[i].size()==0)continue;
            if(!already[i]){
                tmp=tennis[i].front();
                if(i==tennis[tmp].front()&&!already[tmp]){
                    cnt++;
                    tennis[i].pop(); tennis[tmp].pop();
                    match[(res+1)%2].push(i);match[(res+1)%2].push(tmp);
                    already[i]=true;already[tmp]=true;
                    can=true;
                }
            }
        }
        if(cnt==n*(n-1)/2){
            cout<<ans<<endl;
            return 0;
        }
        if(!can){
            cout<<-1<<endl;
            return 0;
        }
        res=(res+1)%2;
    }
}
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 1000010, M = 1010;

int n, m;
int h[N], e[N], ne[N], idx;
int q[N], d[N], st[N];
int g[M][M], match[M][M];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int top_sort()
{
	int hh = 0, tt = -1;
	for (int i = 1; i <= m; i ++ )
		if (!d[i])
		{
			q[ ++ tt] = i;
			st[i] = 1;
		}
	
	while (hh <= tt)
	{
		int t = q[hh ++ ];
		
		for (int i = h[t]; ~i; i = ne[i])
		{
			int j = e[i];
			if ( -- d[j] == 0)
			{
				q[ ++ tt] = j;
				st[j] = st[t] + 1;
			}
		}
	}
	
	int res = 0;
	for (int i = 1; i <= m; i ++ )
		if (!st[i]) return -1;
		else res = max(res, st[i]);
	
	return res;
}

int main()
{
	memset(h, -1, sizeof h);
	
	scanf("%d", &n);
	
	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j < n - 1; j ++ )
		{
			int a = i, b;
			scanf("%d", &b);
			g[i][j] = b;
			
			if (match[a][b]) continue;
			match[a][b] = match[b][a] = ++ m;
		}
		
	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j < n - 2; j ++ )
		{
			int a = match[i][g[i][j]], b = match[i][g[i][j + 1]];
			d[b] ++ ;
			add(a, b);
		}
		
	printf("%d\n", top_sort());
	return 0;
}