#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <utility>
#include <string>
#include <stack>
#include <regex>
#include <numeric>
#include <queue>
#include <set>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

int main(int argc, char const *argv[])
{
  ll n;
  cin >> n;
  vector<queue<ll>> a(n);
  REP(i, n)
  {
    REP(j, n - 1)
    {
      ll tmp;
      cin >> tmp;
      a[i].push(tmp - 1);
    }
  }
  ll cnt = 0;
  ll day = 0;
  ll cnt2 = 0;
  ll tmpcnt;
  vector<vector<bool>> kyou(n * (n - 1) / 2, vector<bool>(n, false));
  ll hito1, hito2;
  set<ll> zenjitu;
  set<ll> zenjitu2;
  while (true)
  {
    day++;
    /*if (cnt - tmpcnt == 1)
    {
      tmpcnt = cnt;
      if (!a[hito1].empty())
      {
        ll next = a[hito1].front();
        if (!a[next].empty())
        {
          if (a[next].front() == hito1)
          {
            a[hito1].pop();
            a[next].pop();
            kyou[day - 1][hito1] = true;
            kyou[day - 1][next] = true;
            cnt++;
            if (cnt == n * (n - 1) / 2)
            {
              cout << day << endl;
              return 0;
            }
          }
        }
      }
      if (!a[hito2].empty())
      {
        ll next = a[hito2].front();
        if (!a[next].empty())
        {
          if (a[next].front() == hito2)
          {
            a[hito2].pop();
            a[next].pop();
            kyou[day - 1][hito2] = true;
            kyou[day - 1][next] = true;
            cnt++;
            if (cnt == n * (n - 1) / 2)
            {
              cout << day << endl;
              return 0;
            }
          }
        }
      }
      if (tmpcnt == cnt)
      {
        cnt2++;
        if (cnt2 == 2)
        {
          cout << -1 << endl;
          return 0;
        }
      }
      else
      {
        cnt2 = 0;
      }
    }*/
    tmpcnt = cnt;
    if (day == 1)
    {
      REP(i, n)
      {
        if (kyou[day - 1][i] || a[i].empty())
        {
          continue;
        }
        ll next = a[i].front();
        if (kyou[day - 1][next] || a[next].empty())
        {
          continue;
        }
        if (a[next].front() == i)
        {
          a[i].pop();
          a[next].pop();
          kyou[day - 1][i] = true;
          kyou[day - 1][next] = true;
          zenjitu.insert(i);
          zenjitu.insert(next);
          cnt++;
        }
        if (cnt == n * (n - 1) / 2)
        {
          cout << day << endl;
          return 0;
        }
      }
    }
    else
    {
      set<ll> zenjitucopy = zenjitu;
      zenjitu.clear();
      for (auto i : zenjitucopy)
      {
        if (kyou[day - 1][i] || a[i].empty())
        {
          continue;
        }
        ll next = a[i].front();
        if (kyou[day - 1][next] || a[next].empty())
        {
          continue;
        }
        if (a[next].front() == i)
        {
          a[i].pop();
          a[next].pop();
          kyou[day - 1][i] = true;
          kyou[day - 1][next] = true;
          zenjitu.insert(i);
          zenjitu.insert(next);
          cnt++;
        }
        if (cnt == n * (n - 1) / 2)
        {
          cout << day << endl;
          return 0;
        }
      }
    }
    if (tmpcnt == cnt)
    {
      cnt2++;
      if (cnt2 == 2)
      {
        cout << -1 << endl;
        return 0;
      }
    }
    else
    {
      cnt2 = 0;
    }
  }
  return 0;
}


Measured memory_bytes: 98816