#include <bits/stdc++.h>
/*
#include<algorithm>
#include<array>
#include<cassert>
#include<cmath>
#include<cstdlib>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<typeinfo>
#include<utility>
#include<vector>
*/
//#define int long long int
#define double long double
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000007
const int MAX_N = 1 << 17;
#define rep(i, n) for (int(i) = 0, i##_len = (n); (i) < i##_len; (i)++)
#define reps(i, x) for (int(i) = 1; (i) <= (int)(x); (i)++)
#define rrep(i, x) for (int(i) = ((int)(x)-1); (i) >= 0; (i)--)
#define rreps(i, x) for (int(i) = ((int)(x)); (i) > 0; (i)--)
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define pb push_back
#define mp make_pair
#define bit(n) ((int)(1) << (n))
#define all(x) (x).begin(), (x).end()
#define debug(x) std::cout << #x << ": " << (x) << std::endl
#define nint int
using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
typedef pair<int, int> pii;
typedef pair<double, double> dop;
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
struct aaa {
    aaa()
    {
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout << fixed << setprecision(20);
    };
} aaaaaaa;


signed main()
{
    int n;
    cin >> n;
    std::vector<std::vector<int>> b(n,std::vector<int>(n - 1));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n - 1; j++){
            int temp;
            cin >> temp;
            temp--;
            b[i][j] = temp;
        }
    }
    
    std::queue<pii> qu;
    std::vector<std::vector<pii>> check(n,std::vector<pii>(n,pii(0,1)));
    std::vector<std::vector<int>> f(n,std::vector<int> (n, 0));
    std::vector<int> index(n,1);
    for(int i = 0; i < n; i++){
        check[i][b[i][0]] = {1,1};
        qu.push(pii(i,b[i][0]));
    }
    int ma = 0;
    int out = 0;
    while(!qu.empty()){
        pii temp = qu.front();
        qu.pop();
        //debug(temp.first);
        //debug(temp.second);
        //cout << endl;
        if(check[temp.first][temp.second].first * check[temp.second][temp.first].first == 1 && f[temp.first][temp.second] + f[temp.second][temp.first] == 0){
        //cout << "koko" << endl;
        //debug(temp.first);
        //debug(temp.second);
        //cout << endl;
            int turn = max(check[temp.first][temp.second].second,check[temp.second][temp.first].second);
            chmax(ma,turn);
            
            pii next,next2;
            if(index[temp.first] < n - 1){
                next = {temp.first,b[temp.first][index[temp.first]]};
            qu.push(next);
            check[temp.first][b[temp.first][index[temp.first]]] = {1,turn + 1};
            }

            if(index[temp.second] < n - 1){
                next2 = {temp.second,b[temp.second][index[temp.second]]};
            qu.push(next2);
            check[temp.second][b[temp.second][index[temp.second]]] = {1,turn + 1};
            }
            f[temp.first][temp.second] = 1;
            f[temp.second][temp.first] = 1;
            index[temp.first]++;
            index[temp.second]++;
            out += 1;
        }
    }
    if(out != n * (n - 1) / 2){
        cout << -1 << endl;
    } else {
        cout << ma << endl;
    }
    return 0;
}

Measured memory_bytes: 16000