#include <bits/stdc++.h>
#include<iostream>
#include <list>
#include <limits.h>
#define pl pair<ll,ll>
#define ll long long
using namespace std;

class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // Pointer to an array containing adjacency lists
    bool isCyclicUtil(int v, bool visited[], bool *rs);  // used by isCyclic()
public:
    Graph(int V);   // Constructor
    void addEdge(int v, int w);   // to add an edge to graph
    bool isCyclic();    // returns true if there is a cycle in this graph
};

Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}

// This function is a variation of DFSUtil() in https://www.geeksforgeeks.org/archives/18212
bool Graph::isCyclicUtil(int v, bool visited[], bool *recStack)
{
    if(visited[v] == false)
    {
        // Mark the current node as visited and part of recursion stack
        visited[v] = true;
        recStack[v] = true;

        // Recur for all the vertices adjacent to this vertex
        list<int>::iterator i;
        for(i = adj[v].begin(); i != adj[v].end(); ++i)
        {
            if ( !visited[*i] && isCyclicUtil(*i, visited, recStack) )
                return true;
            else if (recStack[*i])
                return true;
        }

    }
    recStack[v] = false;  // remove the vertex from recursion stack
    return false;
}

// Returns true if the graph contains a cycle, else false.
// This function is a variation of DFS() in https://www.geeksforgeeks.org/archives/18212
bool Graph::isCyclic()
{
    // Mark all the vertices as not visited and not part of recursion
    // stack
    bool *visited = new bool[V];
    bool *recStack = new bool[V];
    for(int i = 0; i < V; i++)
    {
        visited[i] = false;
        recStack[i] = false;
    }

    // Call the recursive helper function to detect cycle in different
    // DFS trees
    for(int i = 0; i < V; i++)
        if (isCyclicUtil(i, visited, recStack))
            return true;

    return false;
}

// 2nd part starts
bool vis[1000000];
int dp[1000000];
vector<int> adj[1000000];
void dfs(int node)
{
    // Mark as visited
    vis[node] = true;

    // Traverse for all its children
    for (int i = 0; i < adj[node].size(); i++) {

        // If not visited
        if (!vis[adj[node][i]])
            dfs(adj[node][i]);

        // Store the max of the paths
        dp[node] = max(dp[node], 1 + dp[adj[node][i]]);
    }
}

// Function to add an edge
/*void addEdge(vector<int> &adj[], int u, int v)
{
    adj[u].push_back(v);
}*/

// Function that returns the longest path
int findLongestPath( int n)
{
    // Dp array

    memset(dp, 0, sizeof dp);

    // Visited array to know if the node
    // has been visited previously or not

    memset(vis, false, sizeof vis);

    // Call DFS for every unvisited vertex
    for (int i = 0; i < n; i++) {
        if (!vis[i])
            dfs(i);
    }

    int ans = 0;

    // Traverse and find the maximum of all dp[i]
    for (int i = 0; i < n; i++) {
        ans = max(ans, dp[i]);
    }
    return ans;
}
map<pl,ll> get_val;

int main()
{
    // Create a graph given in the above diagram
    ll n;
    cin >> n;
    ll tot= n*(n+1);
    tot /= 2;
    Graph g(tot);

    ll i;
    ll j;
    ll prev;
    ll counter = 0;
    for(i=1;i<=n;i++)
    {

        for(j=i+1;j<=n;j++)
        {

            get_val[{i,j}] = get_val[{j,i}] = counter;
            counter++;
        }
    }
    for(i=1;i<=n;i++)
    {

        for(j=1;j<n;j++)
        {


            ll x;
            cin >> x;

            if(j==1)
            {

                prev = get_val[{i,x}];
                //cout << prev << endl;
            }
            else
            {
                ll temp = get_val[{i,x}];
                g.addEdge(prev, temp);
                adj[prev].push_back(temp);


                prev = temp;
                //cout << prev << endl;


            }

        }


    }


    if(g.isCyclic())
    {

        cout << -1 << endl;
        return 0;

    }
    cout << findLongestPath( tot)+1 << endl;

    return 0;
}


Measured memory_bytes: 146688