#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)
#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); }
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...));
}

template <class T>
inline istream& operator>> (istream &in, vector<T> &vec) {
    for (auto &e : vec) in >> e;
    return in;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(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;
}
// }}} End Header

// graph {{{
struct Edge { // {{{
    int src, to;
    int64_t cost = 0;

    inline Edge() noexcept {}
    inline Edge(int s,  int t, int64_t c = 1) noexcept : src(s), to(t), cost(c) {}

    inline bool operator<(const Edge &rhs) const noexcept { return cost < rhs.cost; }
    inline bool operator>(const Edge &rhs) const noexcept { return rhs < *this; }

    inline operator int() const noexcept { return to; }

    inline Edge& operator=(int rhs_to) noexcept { this->to = rhs_to; return *this; }
};
// }}}

class Graph : public vector<vector<Edge>> { // {{{
    using super = vector<vector<Edge>>;
public:
    Graph() noexcept {}
    Graph(int V) noexcept : super(V) {}

    Graph& addEdge(int from, int to, int64_t cost = 1) noexcept {
        super::operator[](from).emplace_back(from, to, cost);
        return *this;
    }

    Graph& addEdge(const Edge &e) noexcept {
        return addEdge(e.src, e.to, e.cost);
    }

    friend ostream& operator<<(ostream &out, const Graph &g) {
        for (int i = 0; i < g.size(); ++i) {
            out << '[' << setw(2) << i << ']';
            for (const auto &e : g[i]) {
                out << ' ' << setw(2) << e.to;
            }
            out << '\n';
        }
        return out;
    }
}; // }}}
// }}}

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(), -1);

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

            return order;
        }
} // }}}

template<class Compare = less<>, int64_t INF = 0x3f3f3f3f3f3f3f3fLL>
vector<int64_t> dijkstra(const Graph &G, const vector<int> &startNodes) // {{{
{
    using pii = pair<int64_t, int>;
    const auto isBetter = Compare();

    vector<int64_t> dist(G.size(), INF);
    priority_queue<pii, vector<pii>, Compare> pq;

    for (const int startNode : startNodes) {
        dist[startNode] = 0LL;
        pq.emplace(-0LL, startNode);
    }

    while(!pq.empty()) {
        const int64_t nowCost = -(pq.top().first);
        const int     nowNode = pq.top().second;
        pq.pop();

        if (isBetter(dist[nowNode], nowCost)) continue;

        for (const auto &e : G[nowNode]) {
            const int64_t newCost = nowCost + e.cost;
            if (isBetter(newCost, dist[e.to])) {
                dist[e.to] = newCost;
                pq.emplace(-newCost, e.to);
            }
        }
    }

    return dist;
}
// }}}

int N;
vector<vector<int>> A;

int idTable[1005][1005];

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

int memo[1005 * 1005];
int dfs(int u, const Graph &G) {
    if (~memo[u]) return memo[u];
    auto &ret = memo[u];

    ret = 1;
    for (const int nxt : G[u]) {
        chmax(ret, dfs(nxt, G) + 1);
    }
    return ret;
}

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

    read(N);
    A.resize(N);
    rep(i, N) {
        A[i].resize(N - 1);
        rep(j, N - 1) {
            read(A[i][j]);
            --A[i][j];
        }
    }

    {
        int cnt = 0;
        rep(i, N) reps(j, i+1, N-1) {
            idTable[i][j] = cnt++;
        }
    }

    trace(id(N-2, N-1));
    Graph G(id(N-2, N-1) + 1);
    rep(a, N) {
        rep(j, N-2) {
            const int b = A[a][j];
            const int c = A[a][j + 1];
            G.addEdge(id(a, b), id(a, c), -1);
        }
    }

    const auto order = topologicalSort::orderTable(G);
    trace(order);
    if (count(all(order), -1)) {
        print(-1);
        return 0;
    }

    memset(memo, -1, sizeof(memo));
    int ans = 0;
    rep(i, G.size()) chmax(ans, dfs(i, G));

    print(ans);

    return 0;
}


Measured memory_bytes: 51012