#include<bits/stdc++.h>
using namespace std;
#define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define endl '\n'
#define ld long double
#define FOR(i,a,n) for (ll i=(a);i<=(n);++i)
#define RFOR(i,a,n) for (ll i=(n);i>=(a);--i)
#define FI(i,n) for (ll i=0; i<(n); ++i)
#define ZERO(a) memset((a),0,sizeof((a)))
#define MINUS(a) memset((a),-1,sizeof((a)))
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(),g.end()
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }
int fastMin(int x, int y) { return (((y-x)>>(32-1))&(x^y))^x; }
 
/*
░░░░░░░░░░░░░░░░░░░░░░░░░
░░█▀▀▄░░░░░░░░░░░▄▀▀█░░░░
░░█░░░▀▄░▄▄▄▄▄░▄▀░░░█░░░░
░░░▀▄░░░▀░░░░░▀░░░▄▀░░░░░
░░░░░▌░▄▄░░░▄▄░▐▀▀░░░░░░░
░░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█░
░░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█░
░▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█░
░█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀░
░░▀▄░░▀░░▀▀▀░░▀░░░▄█▀░░░░
░░░░█░░░░░░░░░░░▄▀▄░▀▄░░░
░░░░█░░░░░░░░░▄▀█░░█░░█░░
░░░░█░░░░░░░░░░░█▄█░░▄▀░░
░░░░█░░░░░░░░░░░████▀░░░░
░░░░▀▄▄▀▀▄▄▀▀▄▄▄█▀░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░
*/
 
// I want to be the very best, like no one ever was, I will battle everyday to claim my rightful place
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>     // Including tree_order_statistics_node_updat
// using namespace __gnu_pbds;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
 
// I am questioning life and universe and 
// everything else after looking at this

const ll MAXN = 1e7 + 10;
vector<ll> graph[MAXN];
const ll MM = 1e3 + 10;
ll a[MM][MM];
bool visit[MAXN],restack[MAXN];
ll indegree[MAXN];

bool isCycle(ll v){
    if(visit[v] == false){
        visit[v] = true;
        restack[v] = true;

        for(ll u:graph[v]){
            if(!visit[u] && isCycle(u)) return true;
            else if(restack[u]) return true;
        }
    }

    restack[v] = false;
    return false;
}

void solve(){
    ll n; cin>>n;

    FOR(i,1,n){
        FOR(j,1,n-1){
            cin>>a[i][j];
            a[i][j]--;
        }
    }

    FOR(i,1,n){
        FOR(j,1,n-2){
            ll u1 = i-1,u2 = a[i][j];
            ll v1 = i-1,v2 = a[i][j+1];

            if(u1 >= u2) swap(u1,u2);
            if(v1 >= v2) swap(v1,v2);
            
            ll u = u1*1000 + u2;
            ll v = v1*1000 + v2;
            graph[u].push_back(v);
            indegree[v]++;
        }
    }

    bool flag = false;

    FOR(i,0,n*1000 + n-1){
        if(isCycle(i)) {
            flag = true; break;
        }
    }

    if(flag){
        cout<<-1<<endl;
        return;
    }

    set<ll> ss;
    FOR(i,0,n*1000 + n-1){
        if(graph[i].size() && indegree[i] == 0) ss.insert(i);
    }

    ll ans = 0;
    while(ss.size()){
        vector<ll> v;
        for(ll x:ss){
            v.push_back(x);
        }

        for(ll x:v) ss.erase(x);
        for(ll x:v){
            for(ll u:graph[x]){
                indegree[u]--;
                if(indegree[u] == 0) ss.insert(u);
            }
        }
        ans++;
    }

    cout<<ans<<endl;
} 
 
signed main(){
 
   FastRead;    
 
 
    ll t;
    t = 1; 
    // cin>>t;
    FOR(i,1,t){
        // cout<<"Case #"<<i<<": ";
        solve();
    }
}

Measured memory_bytes: 274048