#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second

constexpr int MAXN = 1000 + 42;

int C[MAXN][MAXN];
int S[MAXN], P[MAXN];

int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < N - 1; j++) {
      int a;
      cin >> a;
      C[i][j] = a;
    }
  }
  int K = 0, Q = N * (N - 1) / 2;
  vector<int> R(N);
  iota(R.begin(), R.end(), 1);
  while (Q) {
    if (R.empty()) {
      K = -1;
      break;
    }
    vector<int> T;
    for (int a : R) {
      int b = C[a][S[a]++];
      if (P[b] == a) {
        if (S[a] != N - 1) {
          T.push_back(a);
        }
        if (S[b] != N - 1) {
          T.push_back(b);
        }
        Q--;
      }
      P[a] = b;
    }
    K++;
    R = T;
  }
  cout << K << endl;

  return 0;
}
