#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(Type, ...)  Type __VA_ARGS__; input(__VA_ARGS__)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
inline i64  sigma(i64 n)            { return (n * (n + 1) >> 1); }
inline i64  updiv(i64 a, i64 b)     { return (a + b - 1) / b; }
inline i64  sqr(i64 n)              { return n * n; }
inline string to_string(char c)     { return string(1, c); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void input() {}
template <class Head, class... Tail>
inline void input(Head &head, Tail&... tail) { cin >> head; input(tail...); }

inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}

template <typename Func>
struct FixPoint : Func {
    inline constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {}

    template <typename... Args>
    inline decltype(auto) operator()(Args &&... args) const {
        return Func::operator()(*this, forward<Args>(args)...);
    }
};

template< typename Func >
inline decltype(auto) makeFixPoint(Func &&f) {
    return FixPoint< Func >{forward< Func >(f)};
}
// }}} End Header

#define UNIQUE(a) \
    do{ sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }while(0);

namespace topologicalSort { /// {{{
    namespace {
        template <class Edge> using Graph = vector<vector<Edge>>;
    }

    template <class Edge>
        vector<int> sortedNodeArray(const Graph<Edge> &G) {
            const int V = G.size();
            vector<int> indeg(V, 0);
            vector<int> ans, stk;
            ans.reserve(V);
            stk.reserve(V);

            for (int v = 0; v < V; ++v) {
                for (const int to : G[v]) ++indeg[to];
            }
            for (int v = 0; v < V; ++v) {
                if (indeg[v] == 0) stk.emplace_back(v);
            }

            while (stk.size()) {
                const int v = stk.back(); stk.pop_back();
                ans.emplace_back(v);
                for (const int to : G[v]) {
                    if (--indeg[to] == 0) stk.emplace_back(to);
                }
            }

            return ans;
        }

    template <class Edge>
        vector<int> orderTable(const Graph<Edge> &G) {
            const auto sorted = sortedNodeArray(G);
            vector<int> order(G.size());

            for (int i = 0; i < sorted.size(); ++i) {
                order[sorted[i]] = i;
            }

            return order;
        }
} // }}}

signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    var(int, N);
    auto rival = make_v<int>(-1, N, N-1);

    rep(i, N) rep(j, N - 1) {
        input(rival[i][j]);
        --rival[i][j];
    }

    const auto battleID = [&N](int a, int b) -> int {
        if (a > b) swap(a, b);
        return a * N + b;
    };

    const int V = N * N;
    vector<vector<int>> G(V);

    rep(i, N) {
        rep(j, N-2) {
            const int battle1 = battleID(i, rival[i][j]);
            const int battle2 = battleID(i, rival[i][j + 1]);
            G[battle1].push_back(battle2);
        }
    }
    rep(i, N) UNIQUE(G[i]);

    if (topologicalSort::sortedNodeArray(G).size() < V) {
        print(-1);
        return 0;
    }

    vector<int> dp(V, -1);

    const auto longestPath = makeFixPoint([&](auto dfs, int v) -> int {
        if (~dp[v]) return dp[v];
        dp[v] = 1;
        for (const int to : G[v]) {
            chmax(dp[v], dfs(to) + 1);
        }
        return dp[v];
    });

    int ans = 0;
    rep(i, V) chmax(ans, longestPath(i));

    print(ans);

    return 0;
}


Measured memory_bytes: 54784