#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <unordered_map>
using namespace std;

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

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

vector<int> topological_sort(const vector<vector<Edge> > &graph) {
  int n = graph.size();
  vector<int> deg(n, 0);
  REP(i, n) {
    for (Edge e : graph[i]) ++deg[e.dst];
  }
  queue<int> que;
  REP(i, n) {
    if (deg[i] == 0) que.emplace(i);
  }
  vector<int> res;
  while (!que.empty()) {
    int ver = que.front(); que.pop();
    res.emplace_back(ver);
    for (Edge e : graph[ver]) {
      if (--deg[e.dst] == 0) que.emplace(e.dst);
    }
  }
  return res.size() == n ? res : vector<int>();
};

int n;
int mp1(int a, int b) {
  return a * (2 * n - a - 1) / 2 + (b - a - 1);
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  cin >> n;
  vector<vector<Edge> > graph(n * (n - 1) / 2);
  // map<pair<int, int>, int> mp1;
  unordered_map<int, pair<int, int> > mp2;
  int cnt = 0;
  REP(i, n) FOR(j, i + 1, n) {
    // mp1[{i, j}] = cnt;
    mp2[cnt] = {i, j};
    ++cnt;
  }
  REP(i, n) {
    vector<int> a(n - 1);
    REP(j, n - 1) {
      cin >> a[j]; --a[j];
    }
    REP(j, n - 2) {
      // int src = (i < a[j] ? mp1[{i, a[j]}] : mp1[{a[j], i}]);
      // int dst = (i < a[j + 1] ? mp1[{i, a[j + 1]}] : mp1[{a[j + 1], i}]);
      int src = (i < a[j] ? mp1(i, a[j]) : mp1(a[j], i));
      int dst = (i < a[j + 1] ? mp1(i, a[j + 1]) : mp1(a[j + 1], i));
      graph[src].emplace_back(src, dst);
    }
  }
  vector<int> ts = topological_sort(graph);
  if (ts.empty()) {
    cout << -1 << '\n';
  } else {
    int ans = 1;
    vector<int> cnt(n, 1);
    REP(i, ts.size()) {
      int a = mp2[ts[i]].first, b = mp2[ts[i]].second;
      if (cnt[a] < ans) cnt[a] = ans;
      if (cnt[b] < ans) cnt[b] = ans;
      if (cnt[a] == ans + 1 || cnt[b] == ans + 1) {
        ++ans;
        cnt[a] = ans + 1;
        cnt[b] = ans + 1;
      } else {
        ++cnt[a];
        ++cnt[b];
      }
    }
    cout << ans << '\n';
  }
  return 0;
}


Measured memory_bytes: 62968