#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
}

const int N = 1e3+23;

int n;
int bio[N][N];
int a[N][N];
int dp[N][N];
vector<pair<int, int>> g[N][N];

int dfs(int x, int y) {
  int &ret = dp[x][y];
  if (ret) return ret;
  bio[x][y] = 1;
  ret = 1;
  for (const auto &v: g[x][y]) {
    if (bio[v.first][v.second] == 1) {
      printf("-1");
      exit(0);
    }
    ret = max(ret, 1+dfs(v.first, v.second));
  }
  bio[x][y] = 2;
  return ret;
}

void solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n-1; ++j) {
      scanf("%d", a[i]+j);
      --a[i][j];
      if (j) {
	g[min(i, a[i][j-1])][max(i, a[i][j-1])]
	  .emplace_back(min(i, a[i][j]), max(i, a[i][j]));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i+1; j < n; ++j) {
      if (bio[i][j]) continue;
      ans = max(ans, dfs(i, j));
    }
  }
  printf("%d", ans);

}
int main() {
  init();
  int t = 1; //scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int mod=1e9+7;

int id[1005][1005];
vector<int>to[1000005];
bool visited[1000005];
int dp[1000005];
bool calculated[1000005];

int toid(int i,int j){
  if(i>j){swap(i,j);}
  return id[i][j];
}

int dfs(int v){
  if(visited[v]){
    if(!calculated[v]){
      return -1;
    }
    return dp[v];
  }
  visited[v]=1;
  dp[v]=1;
  for(int u:to[v]){
    int res=dfs(u);
    if(res==-1){return -1;}
    dp[v]=max(dp[v],res+1);
  }
  calculated[v]=1;
  return dp[v];
}

int main() {
  int N;
  cin>>N;
  vector<vector<int>>A(N,vector<int>(N-1));
  for(int i=0;i<N;i++){
    for(int j=0;j<N-1;j++){
      cin>>A[i][j];
      A[i][j]--;
    }
  }
  int v=0;
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
       if(i<j){
         id[i][j]=v++;
       }
    }
  }
  for(int i=0;i<N;i++){
    for(int j=0;j<N-1;j++){
      A[i][j]=toid(i,A[i][j]);
    }
    for(int j=0;j<N-2;j++){
      to[A[i][j+1]].push_back(A[i][j]);
    }
  }
  int ans=0;
  for(int i=0;i<v;i++){
    int res=dfs(i);
    if(res==-1){
      cout<<-1<<endl;return 0;
    }
    chmax(ans,res);
  }
  cout<<ans<<endl;
  return 0;  
}