// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef vector<ll> vec;
typedef vector<P> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<ll,vector<ll>,greater<ll>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;
const vector<int> dx = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const int INF = (1LL << 60); // 1152921504606846976
const double EPS = (1 >> 30);
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }

int max_len;
struct DirectedGraph
{
    int V;
    vvec G; // 有向グラフ
    vvec rG; // 逆向きの有向グラフ

    vector<bool> used;
    vec vs;
    vec depth;

    vec cmp;
    DirectedGraph(int V) : V(V),
        G(vvec(V)),
        rG(vvec(V)),
        cmp(vec(V)),
        depth(vec(V))
        {}
    
    void add_edge(int a,int b){ // a->b
        G[a].pb(b);
        rG[b].pb(a);
    }

    void dfs(int now){
        used[now]=true; // 探索済
        for(auto next:G[now]) if(!used[next]) dfs(next);
        vs.pb(now);
    }

    int rdfs(int now,int k){
        int ret=0;
        used[now]=true;
        cmp[now]=k;
        for(auto next: rG[now]){
            if(!used[next]) rdfs(next,k);
            chmax(depth[now],depth[next]+1); 
        }
        return ret+1;
    }

    int scc_num=-1;
    int scc(){
        used=vector<bool>(V,false);
        vs.clear();
        rep(v,V) if(!used[v]) dfs(v);

        used=vector<bool>(V,false);
        scc_num=0;
        for(int i=vs.size()-1;i>=0;i--){
            if(!used[vs[i]]){
                chmax(max_len,rdfs(vs[i],scc_num++));
            }
        }
        return scc_num; // 強連結成分の個数を返却
    }

    DirectedGraph generateTopologicalGraph()
    {
        DirectedGraph newgraph(scc_num);
        rep(s,V){
            for(auto next:G[s]){
                if(cmp[s]!=cmp[next]){
                    newgraph.add_edge(cmp[s],cmp[next]);
                }
            }
        }
        return newgraph;
    }
};

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    //---------------------------------------------
    
    int N;
    cin >> N;
    DirectedGraph g((N + 1) * (N + 1));
 
    for(int i=1;i< N + 1;i++)
    {
        vector<int> a(N - 1);
        rep(j,N-1) cin>>a[j];
        REP(j, N - 1) a[j] = min(i, a[j]) * N + max(i, a[j]);
        REP(j, N - 2)
        {
            g.add_edge(a[j], a[j + 1]);
        }
    }
    int s = g.scc();
    if (s != (N + 1) * (N + 1))
    {
        puts("-1");
        return 0;
    }
 
    cout << *max_element(all(g.depth)) + 1 << endl;
    
}

Measured memory_bytes: 104044