#include <bits/stdc++.h>
#define range(i, l, r) for(int (i) = (l); (i) < (r); (i)++)
#define reversed_range(i, l, r) for (int (i) = (r) - 1; (i) >= l; (i)--)
using namespace std;
template <typename T>
using vec = vector<T>;
using lint = long long;
using ulint = unsigned long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;

template <typename S, typename T>
ostream& operator <<(ostream& os, pair<S, T> p) {
    os << "(";
    os << p.first << ", " << p.second;
    return os << ")";
}

template <typename T>
ostream& operator <<(ostream& os, vec<T> v) {
    os << "[";
    if (v.size() == 0) return os << "]";
    for (int i = 0; i < v.size() - 1; i++) {
        os << v.at(i) << ", ";
    }
    return os << v.at(v.size() - 1) << "]";
}

template <typename T, typename U>
ostream& operator <<(ostream& os, set<T, U>& s) {
    os << "{";
    if (s.begin() == s.end()) return os << "}";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename T>
ostream& operator <<(ostream& os, unordered_set<T>& s) {
    os << "{";
    if (s.begin() == s.end()) return os << "}";
    auto it_first_item = s.begin();
    cout << *it_first_item;
    for (auto it = ++it_first_item; it != s.end(); it++) {
        cout << ", " << *it;
    }
    return cout << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, map<K, V> m) {
    os << "{";
    if (m.begin() == m.end()) return os << "}";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

template <typename K, typename V>
ostream& operator <<(ostream& os, unordered_map<K, V> m) {
    os << "{";
    if (m.begin() == m.end()) return os << "}";
    auto it_first_item = m.begin();
    cout << it_first_item->first << ": " << it_first_item->second;
    for (auto it = ++it_first_item; it != m.end(); it++) {
        cout << ", " << it->first << ": " << it->second;
    }
    return os << "}";
}

lint pow(lint num, lint e, lint MOD) {
    lint res = 1;
    lint cur_num = num;
    while (e) {
        if (e & 1) {
            res *= cur_num;
            res %= MOD;
        }
        cur_num *= cur_num;
        cur_num %= MOD;
        e >>= 1;
    }
    return res;
}

int main() {
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    lint N;
    cin >> N;
    vec<vec<lint>> match_order(N, vec<lint>(N - 1));
    range(i, 0, N) range(j, 0, N - 1) {
        cin >> match_order.at(i).at(j);
        match_order.at(i).at(j) -= 1;
    }
    // cout << match_order << "\n";
    vec<lint> cur_idx(N, 0);
    auto cmp = [](const vec<lint>& a, const vec<lint>& b) {
        if (a.at(0) != b.at(0)) return a.at(0) > b.at(0);
        else if (a.at(1) != b.at(1)) return a.at(1) < b.at(1);
        else return a.at(2) < b.at(2);
    };
    set<vec<lint>, decltype(cmp)> pairs_in_process(cmp);
    auto insert_item = [&](lint i, lint j) {
        if (i > j) swap(i, j);
        vec<lint> item = {1, i, j};
        if (pairs_in_process.count(item)) {
            pairs_in_process.erase(item);
            item.at(0) += 1;
            pairs_in_process.insert(item);
        } else {
            pairs_in_process.insert(item);
        }
        return;
    };
    range(i, 0, N) {
        lint wanted = match_order.at(i).at(0);
        insert_item(i, wanted);
        // cout << pairs_in_process << "\n";
    }
    // cout << pairs_in_process << "\n";
    lint days_passed = 0;
    while (!pairs_in_process.empty()) {
        vec<lint> first_item = *pairs_in_process.begin();
        if (first_item.at(0) == 1) {
            cout << -1 << "\n";
            return 0;
        }
        vec<vec<lint>> next_pairs;
        while (first_item.at(0) == 2) {
            pairs_in_process.erase(first_item);
            lint i = first_item.at(1), j = first_item.at(2);
            if (cur_idx.at(i) < N - 2) {
                cur_idx.at(i) += 1;
                next_pairs.push_back({i, match_order.at(i).at(cur_idx.at(i))});
            }
            if (cur_idx.at(j) < N - 2) {
                cur_idx.at(j) += 1;
                next_pairs.push_back({j, match_order.at(j).at(cur_idx.at(j))});
            }
            if (pairs_in_process.empty()) break;
            first_item = *pairs_in_process.begin();
        }
        days_passed += 1;
        for (const auto& p : next_pairs) insert_item(p.at(0), p.at(1));
    }
    cout << days_passed << "\n";
}

Measured memory_bytes: 8192