#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
    sizeは(int)とキャストしよう
    cin.tie(0);
    ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう

*/

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);

    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };


// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b){
    double ax=a.first, ay=a.second, bx=b.first, by=b.second;
    return atan2(by-ay, bx-ax);
}

/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */

// 有向グラフでの閉路検出
// O(V+E)
// 0:New, 1:Active, 2:Finished
// Activeは行きがけ順序、Finishedは帰り掛け順序

const int NMAX = 1010;
vector<pair<int, int>> G[NMAX][NMAX];
vector<vector<int>> status(NMAX, vector<int>(NMAX, 0));

bool IsAcyclicDFS(int i, int j){
    status[i][j] = 1;
    for(auto p : G[i][j]){
        int ni = p.first, nj = p.second;
        if(status[ni][nj] == 1) return false;
        else if(status[ni][nj] == 0){
            if(IsAcyclicDFS(ni, nj) == false) return false;
        }
    }
    status[i][j] = 2;
    return true;
}

bool IsAcyclic(){
    rep(i, NMAX) rep(j, NMAX) status[i][j] = 0;
    rep(i, NMAX) rep(j, NMAX) {
        if(status[i][j] == 0){
            if(IsAcyclicDFS(i, j) == false) return false;
        }
    }
    return true;
}


vector<vector<int>> memo(1010, vector<int>(1010, -1));

int rec(int i, int j){
    if(memo[i][j] != -1) return memo[i][j];
    
    int res = 1;
    for(auto p : G[i][j]){
        int ni = p.first, nj = p.second;{
            chmax(res, rec(ni, nj)+1);
        }
    }
    return memo[i][j] = res;
}


int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    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]; }

    rep(i, n) repr(j, 1, n-1) {
        int p1 = i, p2 = a[i][j-1];
        int p3 = i, p4 = a[i][j];
        if(p1 > p2) swap(p1, p2);
        if(p3 > p4) swap(p3, p4);
        G[p1][p2].push_back(make_pair(p3, p4));
    }

    if(IsAcyclic() == false){
        cout << -1 << endl;
        return 0;
    }

    int ans = 0;
    rep(i, n) repr(j, i+1, n) chmax(ans, rec(i, j));
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 51840