//code by lynmisakura.wish to be accepted!
/****************************/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<bitset>
#include <climits>
#include<set>
#include<bitset>
using namespace std;
/***************************/
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<pair<int,int>> vpi;

const long long INF = 1LL << 55;

#define itn int
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
#define all(x) (x).begin(),(x).end()
#define Rep(n) for(int i = 0;i < n;i++)
#define rep(i,n) for(int i = 0;i < n;i++)
#define rrep(i,n) for(int i = n - 1;i >= 0;i--)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )
 
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll qp(ll a, ll b) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a; a = 1ll * a*a; } while (b >>= 1); return ans; }
ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a%mo; a = 1ll * a*a%mo; } while (b >>= 1); return ans; }
 
#define _GLIBCXX_DEBUG
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//mint
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x(x%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

/******************************************/
int want[1010];
int used[1010];
/*****************************************/

int main(void){
   cin.tie(0);
   ios::sync_with_stdio(false);
   
   int n;cin >> n;
   vector<queue<int>> Q(n);
   rep(i,n){
      rep(j,n-1){
         int p;cin >> p;
         p--;
         Q[i].push(p);
      }
   }
   //multiset<int> s;
   rep(i,n){
      want[i] = Q[i].front();
   }
   int day = 1;
   // queue<int> next;
   while(true){
      // for(auto i : s) cout << i << " ";
      // cout << endl;
      bool check = false;
      int e = 0;
      fill(used,used + n,0);
      for(int i = 0;i < n;i++){
         if(Q[i].empty()){
            e++;
            continue;
         }
         int x = i,y = want[i];
         if(used[x] || used[y]) continue;
         if(want[y] == x){
            used[x] = used[y] = 1;
            Q[x].pop();
            Q[y].pop();
            if(!Q[x].empty())want[x] = Q[x].front();else want[x] = -1;
            if(!Q[y].empty())want[y] = Q[y].front();else want[y] = -1;
            check = true;
         }
         
      }
      
      if(e == n) break;
      if(!check){
         cout << -1 << endl;
         return 0;
      }
      day++;
   }
  
   cout << day - 1 << endl;
   return 0;
}

Measured memory_bytes: 4736