// clang-format off
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>

#include <algorithm>
#include <numeric>
#include <limits>
#include <chrono>
#include <functional>
#include <iterator>
#include <random>
#include <complex>
#include <utility>
#include <type_traits>
#include <tuple>
#include <memory>

#include <vector>
#include <array>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <valarray>

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <csignal>
#include <cstdint>
#include <cstddef>

#ifdef ONLINE_JUDGE
#define NDEBUG
#endif
#include <cassert>
#include <stdexcept>
#include <exception>

// << pair
template <typename T1, typename T2>std::ostream&operator<<(std::ostream&os,const std::pair<T1,T2>&p){return os<<"("<<p.first<<", "<<p.second<<")";}
// << container
template<typename,template<typename...> class...>struct is_instantiation_of:public std::false_type{};
template<template<typename...>class U,template<typename...>class...Us,typename...T>struct is_instantiation_of<U<T...>,U,Us...>:public std::true_type{};
template<typename S,template<typename...>class U,template<typename...>class...Us>struct is_instantiation_of<S,U,Us...>:std::integral_constant<bool,is_instantiation_of<S,Us...>::value>{};

template<typename C>std::ostream&_debug_go(std::ostream&,const C&);
template<typename C,typename=std::enable_if_t<is_instantiation_of<C,std::vector,std::set,std::deque,std::map,std::unordered_set,std::unordered_map,std::multiset,std::multimap,std::unordered_multiset,std::unordered_multimap,std::valarray>::value>>std::ostream&operator<<(std::ostream&os,const C&v){return _debug_go(os,v);}
template<typename T,std::size_t N> std::ostream& operator<<(std::ostream&os,const std::array<T,N>&v){return _debug_go(os,v);}
template<typename C>std::ostream&_debug_go(std::ostream&os,const C& v){bool first=true;os<<"[";for(auto i:v){if(!first)os<<", ";else first=false;os<<i;}return os<<"]";}

struct Debugger{template<typename T>Debugger&operator,(const T&v){std::cerr<<v<<" ";return *this;}} dbg;
#ifndef NDEBUG
#define debug(args...) do {dbg,args; cerr << "\n";} while(0)
#else
#define debug(args...)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ITER(i, s, e) for (auto i((s)); i != (e); ++i)
#define IT(i, c) ITER(i, (c).begin(), (c).end())

using namespace std;

using ui = uint32_t;
using ll = int64_t;
using ull = uint64_t;
template <typename T = int> using Pt = complex<T>;

template <typename T>
T cross(const Pt<T>& a, const Pt<T>& b) { return a.real() * b.imag() - a.imag() * b.real(); }

template <typename T>
bool cmpPt(const Pt<T>& a, const Pt<T>& b) {
  return a.real() < b.real() || (a.real() == b.real() && a.imag() < b.imag());
}
// END TEMPLATE
// clang-format on

int solve(int N, const vector<vector<int>>& A) {
  int days;
  vector<int> done(N), vis(N);
  // debug(A);
  for (days = 0;; ++days) {
    if (*min_element(done.begin(), done.end()) == N - 1) break;
    bool progress = false;
    fill(vis.begin(), vis.end(), 0);
    REP(i, N) {
      // debug(days, done[i] < N - 1 ? A[i][done[i]] : -1);
      if (vis[i]) continue;
      vis[i] = true;
      if (done[i] >= N - 1) continue;
      int j = A[i][done[i]];
      if (vis[j] || A[j][done[j]] != i) continue;
      // debug(days, i, j);
      progress = vis[j] = true;
      ++done[i];
      ++done[j];
    }
    // debug(days, done, progress);
    if (!progress) return -1;
  }
  return days;
}

int solveFaster(int N, const vector<vector<int>>& A) {
  deque<pair<int, int>> q;
  REP(i, N) q.push_back({-1, i});
  int days;
  vector<int> done(N), vis(N, -1);
  for (days = 0; !q.empty(); ++days) {
    while (!q.empty() && q.front().first < days) {
      int i = q.front().second;
      q.pop_front();
      if (vis[i] >= days || done[i] >= N - 1) continue;
      int j = A[i][done[i]];
      if (vis[j] >= days || done[j] >= N - 1 || A[j][done[j]] != i) continue;
      vis[i] = vis[j] = days;
      ++done[i];
      ++done[j];
      if (done[i] < N - 1) q.push_back({days, i});
      if (done[j] < N - 1) q.push_back({days, j});
    }
  }
  if (*min_element(done.begin(), done.end()) < N - 1) return -1;
  return days;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(N - 1));
  REP(i, N) REP(j, N - 1) cin >> A[i][j], --A[i][j];

  cout << solveFaster(N, A) << '\n';

  return 0;
}


Measured memory_bytes: 4224