#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define ms multiset
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("input.txt");ofstream out("output.txt");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);

/*

int a,k,a1,b1,a2,b2, sz=1;
vec<vec<int>> l, n, m;
map<pii, int> mp;
vec<int> dst, uss;
vec<pii> v;

void bfs(int sv){
    try {
        if (l[sv].size() == 0) return;
        pii st = {sv, l[sv][0]};
        mp[st] = sz++;
        v.pb(st);
        deque<pii > d = {st};
        for (; d.size();) {
            pii p = d[0]; d.pop_front();
            int num = mp[p] - 1;
            for (int h : l[p.F]) {
                if (h == p.S) continue;
                pii nw = {h, p.F};
                if (!mp[nw]) mp[nw] = sz++, v.pb(nw), d.pb(nw);
                n[num].pb(mp[nw] - 1);
                if (m[h][p.S]) {
                    nw = {h, p.S};
                    if (!mp[nw]) mp[nw] = sz++, v.pb(nw), d.pb(nw);
                    n[num].pb(mp[nw] - 1);
                }
            }
            for (int h : l[p.S]) {
                if (h == p.F) continue;
                pii nw = {p.S, h};
                if (!mp[nw]) mp[nw] = sz++, v.pb(nw), d.pb(nw);
                n[num].pb(mp[nw] - 1);
                if (m[p.F][h]) {
                    nw = {p.F, h};
                    if (!mp[nw]) mp[nw] = sz++, v.pb(nw), d.pb(nw);
                    n[num].pb(mp[nw] - 1);
                }
            }
        }
    }
    catch (...){
        for(ll q=0; q<1e15; q++) sz+=rnd();
    }
}

void cnt_dst(int st){
    deque<int> d = {st};
    vec<int> us(sz);
    int szz = 1, cnt=0, ds = 0;
    us[st] = 1;
    for(; d.size(); cnt++){
        if(cnt==szz) szz = d.size(), cnt=0, ds++;
        int x = d[0]; d.pop_front();
        dst[x] = ds;
        for(int i : n[x]){
            if(!us[i]) us[i] = 1, d.pb(i);
        }
    }
}

int get_wei(pii p1, pii p2){
    return 2-(p1.F==p2.F)-(p1.S==p2.S);
}

void dfs(int vv){
    uss[vv] = 1;
    for(int h : l[vv]) if(!uss[h]) dfs(h);
}

int main(){
    fast;
    cin>>a>>k>>a1>>b1>>a2>>b2; a1--, a2--, b1--, b2--;
    uss = vec<int>(a);
    l = vec<vec<int>>(a, vec<int>());
    n = vec<vec<int>>(100005, vec<int>());
    m = vec<vec<int>>(a, vec<int>(a));
    for(int q=0; q<k; q++){
        int x,y; cin>>x>>y; x--; y--;
        m[x][y] = m[y][x] = 1;
        l[x].pb(y);
        l[y].pb(x);
    }
    for(int q=0; q<a; q++){
        if(!uss[q]){
            dfs(q);
            bfs(q);
        }
    }
    sz--;
    for(; n.back().size()==0;) n.pop_back();
    //for(int q=0; q<sz; q++){cout<<q+1<<": "; for(int h : n[q]) cout<<h+1<<" "; cout<<endl;}
    int s = mp[{a1, b1}]-1, f = mp[{a2, b2}]-1;
    if(s<0 || f<0){
        for(ll q=0; q<1e15; q++) sz+=rnd();
    }
    dst = vec<int>(sz);
    cnt_dst(s);
    int inf = 1e8;
    vec<int> d(sz, inf), us(sz);
    d[s] = 0;
    pq<pii, vec<pii>, greater<pii>> p;
    p.push({0, s});
    for(; p.size(); ){
        pii u = p.top(); p.pop();
        int c = u.F, x = u.S;
        if(us[x]) continue;
        us[x] = 1;
        for(int h : n[x]){
            if(c+get_wei(v[x], v[h]) < d[h]){
                d[h] = c+get_wei(v[x], v[h]);
                p.push({d[h], h});
            }
        }
    }
    //for(pii p : v) cout<<"{"<<p.F+1<<", "<<p.S+1<<"} "; cout<<endl;
    //for(int i : d) cout<<i<<" "; cout<<endl;
    cout<<d[f]<<" "<<dst[f]+1;
}

*/

int a;
vec<deque<int>> m;

int main(){
    fast;
    cin>>a;
    m = vec<deque<int>>(a, deque<int>(a-1));
    for(int q=0; q<a; q++){
        for(int w=0; w<a-1; w++) cin>>m[q][w], m[q][w]--;
    }
    int o = 0, all = a*(a-1)/2;
    vec<int> us(a, -1), loxi;
    for(int q=0; q<a; q++) loxi.pb(q);
    for(; all; o++){
        int fl = 0;
        vec<int> eso_loxi;
        for(int i : loxi) {
            if (m[i].size() == 0 || us[i] == o) continue;
            if (m[m[i][0]].size() == 0 || us[m[i][0]] == o) continue;
            if (m[m[i][0]][0] != i) continue;
            us[i] = us[m[i][0]] = o;
            eso_loxi.pb(i);
            eso_loxi.pb(m[i][0]);
            m[m[i][0]].pop_front();
            m[i].pop_front();
            all--;
            fl = 1;
        }
        loxi = eso_loxi;
        if(fl==0){
            o=-1;
            break;
        }
    }
    cout<<o;
}

Measured memory_bytes: 4608