#include <bits/stdc++.h>
using namespace std;

// DAG
vector<bool> visited;
vector<int> dp;

int DFS(const vector<vector<int>> &graph, int v){
    if(visited[v] == true) return dp[v];
    
    visited[v] = true;
    for(auto next_v : graph[v]){
        if(DFS(graph, next_v) == -1) return -1;

        dp[v] = max(dp[v], DFS(graph, next_v) + 1);
    }
    return dp[v] = max(0, dp[v]);// if no next_v (i.e. source) dp[v] = 0
}

int main(){
    int n;
    cin >> n;
    int V = n * (n-1) / 2;
    visited.assign(V,false);
    dp.assign(V,-1);

    vector<vector<int>> a(n, vector<int>(n-1));
    for(int i=0; i<n; i++){
        for(int j=0; j<n-1; j++){
            cin >> a[i][j];
            a[i][j] -= 1;
        }
    }

    vector<vector<int>> matchID(n, vector<int>(n));
    int id = 0;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            matchID[i][j] = id;
            matchID[j][i] = id;
            id += 1;
        }
    }

    vector<vector<int>> graph(V);
    for(int i=0; i<n; i++){
        for(int j=1; j<n-1; j++){
            int s = matchID[i][a[i][j-1]];
            int t = matchID[i][a[i][j]];
            //cout << s << " " << t << endl;
            graph[t].push_back(s);// arrow inversed
        }
    }

    int ans = 0;
    for(int i=0; i<V; i++){
        int depth = DFS(graph, i);
        if(depth == -1){
            cout << -1 << endl;
            return 0;
        }
        ans = max(ans, depth);
    }
    cout << ans + 1 << endl;
    return 0;
}
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main(){
	int n;
  	cin >> n;
  	vector<vector<int>> a(n, vector<int>(n-1));
  	rep(i, n){
    	rep(j, n-1){
          	cin >> a[i][j];
      		a[i][j]--;
        }
      	reverse(a[i].begin(), a[i].end());
    }
  	vector<P> q;
  
  	auto check = [&](int i){
      	if(a[i].size() == 0) return;
      	int j = a[i].back();
      	if(a[j].size() == 0) return;
      	if(a[j].back() == i){
          	P p(i, j);
          	if(p.second < p.first) swap(p.first, p.second);
        	q.push_back(p);
        }
    };

  	rep(i, n){
      	check(i);
    }
  
  	int day = 0;
  	while(q.size() > 0){
  		day++;
      	sort(q.begin(), q.end());
      	q.erase(unique(q.begin(), q.end()), q.end());
      	vector<P> prevQ;
      	swap(prevQ, q);
      	for(P p : prevQ){
        	int i = p.first, j = p.second;
          	a[i].pop_back();
          	a[j].pop_back(); 	
        }
        for(P p : prevQ){
        	int i = p.first, j = p.second;
          	check(i);
          	check(j);
        }
    }
  	
  	rep(i, n){
    	if(a[i].size() != 0){
        	puts("-1");
          	return 0;
        }
    }
  
  	cout << day << endl;
}