#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define pii std::pair<int,int>
#define pli std::pair<ll,int>
#define pil std::pair<int,ll>
#define psi std::pair<string,int>
#define pll std::pair<ll,ll>
#define pci std::pair<char,int>
#define sll(x) scanf("%lld",&x)
#define prll(x) printf("%lld ",x)
#define pri(x) printf("%d ",x)
#define si(x) scanf("%d",&x)
#define pb push_back
#define vll std::vector<ll>
#define vpi std::vector<std::pair<int,int>>
#define vi std::vector<int>
#define vvi std::vector<std::vector<int>>
#define vvpil std::vector<std::vector<std::pair<int,ll>>>
#define vlpii std::vector<std::list<pii>>
#define vlpil std::vector<std::list<pil>>
#define li std::list<int>
#define lpil std::list<pil>
#define Endl printf("\n")
#define vli vector<list<int>>
#define vvll vector<vector<ll>>
#define mp make_pair
#define x first
#define y second
#define ull uint64_t
#define ma 100000000
#define imie(...) "[" << #__VA_ARGS__": " << (__VA_ARGS__) << "] "
const ll  INF=10000000000000;
const ll mod =  1e9 + 7;
using namespace std;
void computeLPSArray(string pat, int M, int* lps); 
void add(ll& a , ll b){
	a += b;
	if(a >= mod) a -= mod;
}
ll mul(ll a , ll b){
	return (a * b) % mod;
}
ll Pow(ll a , ll b){
	if(b == 0) return 1 ;
	ll c = Pow(a , b / 2) ;
	if(b % 2) return (a * ((c * c)% mod )) % mod ;
	return (c * c) % mod ;
}
int n;
vector<vector<int>> adj;
vector<char> color;
vector<int> parent;
int cycle_start, cycle_end;

bool dfs(int v) {
    color[v] = 1;
    for (int u : adj[v]) {
        if (color[u] == 0) {
            parent[u] = v;
            if (dfs(u))
                return true;
        } else if (color[u] == 1) {
            cycle_end = v;
            cycle_start = u;
            return true;
        }
    }
    color[v] = 2;
    return false;
}

bool find_cycle() {
    color.assign(n, 0);
    parent.assign(n, -1);
    cycle_start = -1;

    for (int v = 0; v < n; v++) {
        if (dfs(v))
            break;
    }

    if (cycle_start == -1) {
		return false ;
        cout << "Acyclic" << endl;
    } else {
		 return true ;
        vector<int> cycle;
        cycle.push_back(cycle_start);
        for (int v = cycle_end; v != cycle_start; v = parent[v])
            cycle.push_back(v);
        cycle.push_back(cycle_start);
        reverse(cycle.begin(), cycle.end());

        cout << "Cycle found: ";
        for (int v : cycle)
            cout << v << " ";
        cout << endl;
       
    }
}
vector<bool> visited;
vector<int> ans;

void dfs1(int v) {
    visited[v] = true;
    for (int u : adj[v]) {
        if (!visited[u])
            dfs1(u);
    }
    ans.push_back(v);
}

void topological_sort() {
    visited.assign(n, false);
    ans.clear();
    for (int i = 0; i < n; ++i) {
        if (!visited[i])
            dfs1(i);
    }
    reverse(ans.begin(), ans.end());
    
}
//check modulo
int main(){
	IOS;
	cout.precision(50) ;
	int m ; cin >> m ;
	n = (m * (m - 1)) / 2 ;
	map<pii ,int> maap ;
	int p = 1 ;
	vector<vi> a(m , vi(m - 1)) ;
	int x ; 
	adj.resize(n) ;
	vector<pii> v(n) ;
	for(int i = 0 ; i < m ; i++){
		for(int j = 0 ; j < m - 1  ; j++){
			cin >> x ;
			x-- ;
			if(i < x){maap[mp(i , x)] = p ;v[p - 1] = mp(min(x , i),max(x, i)) ;p++ ;}
			a[i][j] = maap[mp(min(x ,i) , max(x ,i))] ;
			
			if(j == 0) continue ;
			else adj[a[i][j -1] - 1].pb(a[i][j] - 1) ;
		}
		
	}
	if(find_cycle()) return cout << -1 << endl , 0 ;
	topological_sort() ;
	vi days(m , 0) ;
	for(int j = 0 ; j < (int)ans.size() ; j++){
		//cout << ans[j] << " "  ;
		int p1 = v[ans[j]].x , p2 = v[ans[j]].y ;
		//cout << p1 << " " << p2 << " " ;
		ll day = max(days[p1] , days[p2]) ;
		days[p1] = day + 1;
		days[p2] = day + 1 ;
		//cout << day << endl ;
	}
	cout << *max_element(days.begin() , days.end())  << endl ;
			
	return 0;
}


Measured memory_bytes: 71412