#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e10;
const int MOD = 1e9 + 7;
#define dump(x) cout << #x << " = " << (x) << endl;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define P pair<int, int>
#define V vector<int>
#define M map<int, int>
#define S set<int>
#define itn int
#define tin int
#define tni int
#define x first
#define y second
bool dbg = false;
int N, A[1100][1100];
int C[1100][1100];
vector<P> G[1100][1100];
int T[1100][1100];
void solve()
{
    FOR(i, 0, N)
    {
        FOR(j, 0, N - 2)
        {
            G[i][A[i][j]].pb(make_pair(min(i, A[i][j + 1]),max(i,A[i][j+1])));
            G[A[i][j]][i].pb(make_pair(min(i, A[i][j + 1]),max(i,A[i][j+1])));
            T[i][A[i][j + 1]]++;
            T[A[i][j + 1]][i]++;
            //dump(i);
            //dump(j);
            //cout << i << " " << A[i][j + 1] << endl;
        }
    }
    queue<P> s1;
    FOR(i, 0, N)
    {
        FOR(j, i + 1, N)
        {
            if (T[i][j] == 0) {
                s1.push(make_pair(i, j));
                //cout << i << j << endl;
            }
        }
    }
    int cnt = 0;
    if (s1.empty()) {
        cout << -1 << endl;
        return;
    }
    while (!s1.empty()) {
        if (cnt > N) {
            cout << -1 << endl;
            return;
        }
        cnt++;
        //dump(cnt);
        queue<P> s2;
        while (!s1.empty()) {
            P p = s1.front();
            //cout << p.x << " " << p.y << endl;
            s1.pop();
            if (p.x > p.y) {
                continue;
            }
            int X = p.x;
            int Y = p.y;
            C[X][Y] = cnt;
            FOR(i, 0, SZ(G[X][Y]))
            {
                P g = G[X][Y][i];
                //if (g.x < g.y) {
                //cout << g.x << " " << g.y << endl;
                s2.push(mp(g.x, g.y));
                //}
            }
        }
        s1 = s2;
    }
    cout << cnt << endl;
}

signed main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> N;
    FOR(i, 0, N)
    {
        FOR(j, 0, N - 1)
        {
            cin >> A[i][j];
            A[i][j]--;
        }
    }
    solve();
    return 0;
}
