#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
// iteration and input/output stream
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define srep(i, begin, end) for (__typeof(end) i = begin; i != end; i++)
#define printarr(x) rep(i,0,x.size()) cout<<x[i]<<" "; cout<<endl;

#define tr(cont,it) \
for(decltype(cont.begin()) it = cont.begin(); it!= cont.end() ; it++)

using namespace std;

#define tc() int tcase; cin >> tcase ; rep(tc,1,tcase+1)
//STL shortcuts
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define F first
#define S second
#define MP make_pair

// Error detection
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

typedef long long int ll; // 64 bit integer
typedef unsigned long long int  ull; // 64 bit unsigned integer
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<int,ll> pill;

inline void smax(int &x , int y) { x = max(x , y) ; }
inline void smin(int &x , int y) { x = min(x , y) ; }

#define MOD 1000000007
#define inf 4000000000000000000LL
#define SYNC std::ios::sync_with_stdio(false);  cin.tie(NULL);

ll gcd(ll a,ll b){ return ((b==0)? a:gcd(b,a%b) ); }


int main()
{
    SYNC
    ll n;
    cin>>n;
    vector<set<int> > nx(n+1);
    vector<vi> v(n+1);

    vector<pii> ord;
    deque<pii> mat;
    rep(i,1,n+1)
    {
        rep(j,0,n-1)
        {
            int x;
            cin>>x;
            v[i].pb(x);
        }
        reverse(v[i].begin(),v[i].end());
        nx[i].insert(v[i].back());
        if(nx[v[i].back()].find(i) != nx[v[i].back()].end())
        {
            mat.pb(MP(i,v[i].back()));
            ord.pb(MP(i,v[i].back()));
            //nx[v[i].back() ].erase();
        }

    }



    int f =0;
    set<pii> tot;
    while(mat.size())
    {
        int x = mat.front().F;
        int y = mat.front().S;
        mat.pof();
        //trace2(x,y);
        if(tot.find(MP(min(x,y),max(x,y))) != tot.end())
        {
            f=1;
            break;
        }
        tot.insert(MP(min(x,y),max(x,y)));

        nx[x].erase(y);
        nx[y].erase(x);
        v[x].pob();
        v[y].pob();

        if(v[x].size() > 0)
        {
            nx[x].insert(v[x].back());
            if(nx[v[x].back()].find(x) != nx[v[x].back()].end())
            {
                mat.pb(MP(x,v[x].back()));
                ord.pb(MP(x,v[x].back()));
            }
        }
        if(v[y].size() > 0)
        {
            nx[y].insert(v[y].back());
            if(nx[v[y].back()].find(y) != nx[v[y].back()].end())
            {
                mat.pb(MP(y,v[y].back()));
                ord.pb(MP(y,v[y].back()));
            }

        }


    }

    if(f==1 || tot.size() != ((n-1)*n)/2 )
    {
        cout<<-1;
    }
    else
    {
        set<int> cur;
        int day = 1;
        rep(i,0,ord.size())
        {
            int x = ord[i].F;
            int y = ord[i].S;
            if(cur.find(x)!= cur.end() || cur.find(y)!= cur.end())
            {
                day++;
                cur.clear();
                cur.insert(x);
                cur.insert(y);
            }
            else
            {
                cur.insert(x);
                cur.insert(y);
            }
        }
        cout<<day<<endl;
    }


}


Measured memory_bytes: 31724