#include <bits/stdc++.h>

template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
                                             InputIterator last) {
  return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
  for (auto &i : v) {
    stream >> i;
  }
  return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
  stream >> p.first >> p.second;
  return stream;
}

int main() {
  int64_t n;
  std::cin >> n;
  std::vector<std::vector<int64_t>> a(n, std::vector<int64_t>(n - 1));
  std::cin >> a;

  for (auto &i : a) {
    for (auto &j : i) {
      j--;
    }
  }

  std::set<std::pair<int64_t, int64_t>> p;
  std::vector<int64_t> index(n, 0);
  int64_t day = 0;
  for (int64_t i = 0; i < n; i++) {
    p.insert(std::make_pair(i, a[i][0]));
  }

  std::set<std::pair<int64_t, int64_t>> can;

  for (int64_t i = 0; i < n; i++) {
    if (i < a[i][0]) {
      auto itr = p.find(std::make_pair(a[i][0], i));
      if (itr != p.cend()) {
        const auto j = a[i][0];
        index[i]++;
        index[j]++;
        p.erase(std::make_pair(j, i));
        p.erase(std::make_pair(i, j));
        can.insert(std::make_pair(i, a[i][1]));
        can.insert(std::make_pair(j, a[j][1]));
      }
    }
  }

  while (true) {
    std::set<std::pair<int64_t, int64_t>> next;
    bool flag = false;

    std::set<std::pair<int64_t, int64_t>> erace;

    for (auto itr = can.begin(); itr != can.end(); itr++) {
      const auto i = itr->first;
      const auto j = itr->second;
      if (i < j && can.find(std::make_pair(j, i)) != can.end()) {
        flag = true;
        index[i]++;
        index[j]++;
        erace.insert(std::make_pair(i, j));
        if (index[i] != n - 1) {
          next.insert(std::make_pair(i, a[i][index[i]]));
        }
        if (index[j] != n - 1) {
          next.insert(std::make_pair(j, a[j][index[j]]));
        }
      }
      if (p.find(std::make_pair(j, i)) != p.end()) {
        flag = true;
        index[i]++;
        index[j]++;
        erace.insert(std::make_pair(i, j));
        if (index[i] != n - 1) {
          next.insert(std::make_pair(i, a[i][index[i]]));
        }
        if (index[j] != n - 1) {
          next.insert(std::make_pair(j, a[j][index[j]]));
        }
      }
    }
    for (const auto v : erace) {
      const auto i = v.first;
      const auto j = v.second;

      p.erase(std::make_pair(j, i));
      p.erase(std::make_pair(i, j));
      can.erase(std::make_pair(j, i));
      can.erase(std::make_pair(i, j));
    }

    for (const auto v : can) {
      p.insert(v);
    }
    std::swap(next, can);
    day++;
    if (!flag) {
      break;
    }
  }

  bool ansf = true;

  for (const auto i : index) {
    if (i != n - 1) {
      ansf = false;
    }
  }

  if (ansf) {
    std::cout << day << std::endl;
  } else {
    std::cout << -1 << std::endl;
  }

  return 0;
}

Measured memory_bytes: 8192