using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        var wrong_answer = new wrong_answer();
        wrong_answer.Solve();
    }
}

public class wrong_answer
{
    const int INF = 1 << 30;
    const long INFL = 1L << 60;
    const int MOD = 1000000007;
    const int MOD2 = 998244353;

    myinout IO = new myinout();
    Math2 mmath = new Math2();

    //functions
    List<int>[] to;
    bool[] visited;
    bool[] calculated;
    int[] dp;
    int[][] id;
    public int dfs(int v)
    {
        if (visited[v])
        {
            if (!calculated[v]) return -1;
            return dp[v];
        }
        visited[v] = true;
        dp[v] = 1;
        foreach (var x in to[v])
        {
            int res = dfs(x);
            if (res == -1) return -1;
            dp[v] = Math.Max(dp[v], res + 1);
        }
        calculated[v] = true;
        return dp[v];
    }



    public void Solve()
    {
        //ここから
        int n = IO.int1;
        var a = Enumerable.Range(0, n).Select(x => IO.intm.Select(y => y - 1).ToArray()).ToArray();
        int v = 0;
        id = Enumerable.Range(0, n).Select(_ => new int[n]).ToArray();
        visited = new bool[n*n];
        calculated = new bool[n*n];
        dp = new int[n*n];
        to = Enumerable.Range(0, n*n).Select(_ => new List<int>()).ToArray();
        for (int i = 0; i < n;++i){
            for (int j = 0; j < n;++j)calculated[i * j] = false;
        }
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (i < j)
                    {
                        id[i][j] = v;
                        v++;
                    }
                }
            }
        Func<int, int, int> toid = (x, y) => { if (x > y) mmath.swap(ref x, ref y); return id[x][y]; };
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n - 1; ++j)
            {
                a[i][j] = toid(i, a[i][j]);
            }
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n - 2; ++j)
            {
                to[a[i][j + 1]].Add(a[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < v; ++i)
        {
            int res = dfs(i);
            if (res == -1)
            {
                IO.wri(-1);
                return;
            }
            ans = Math.Max(ans, res);
        }

        IO.wri(ans);
    }

}

//priority queue
class PriorityQueue<T>
{
    private readonly List<T> heap;
    private readonly Comparison<T> compare;
    private int size;
    public PriorityQueue() : this(Comparer<T>.Default) { }
    public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
    public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
    public PriorityQueue(int capacity, Comparison<T> comparison)
    {
        this.heap = new List<T>(capacity);
        this.compare = comparison;
    }
    public void Enqueue(T item)
    {
        this.heap.Add(item);
        var i = size++;
        while (i > 0)
        {
            var p = (i - 1) >> 1;
            if (compare(this.heap[p], item) <= 0)
                break;
            this.heap[i] = heap[p];
            i = p;
        }
        this.heap[i] = item;

    }
    public T Dequeue()
    {
        var ret = this.heap[0];
        var x = this.heap[--size];
        var i = 0;
        while ((i << 1) + 1 < size)
        {
            var a = (i << 1) + 1;
            var b = (i << 1) + 2;
            if (b < size && compare(heap[b], heap[a]) < 0) a = b;
            if (compare(heap[a], x) >= 0)
                break;
            heap[i] = heap[a];
            i = a;
        }
        heap[i] = x;
        heap.RemoveAt(size);
        return ret;
    }
    public T Peek() { return heap[0]; }
    public int Count { get { return size; } }
    public bool Any => size > 0;
}


//b-aで降順

public class Math2
{
    public long[] fact;
    public long[] invfact;
    public void factoricalmodset(long N, long MOD = 1000000007)
    {
        fact = new long[N + 1];
        invfact = new long[N + 1];
        fact[0] = 1;

        for (long i = 1; i <= N; ++i)
        {
            fact[i] = fact[i - 1] * i % MOD;
        }
        invfact[N] = pow(fact[N], MOD - 2);

        for (long i = N; i > 0; --i)
        {
            invfact[i - 1] = invfact[i] * i % MOD;
        }

    }

    public long nCr(long n, long r, long MOD = 1000000007)
    {
        return (fact[n] * invfact[r] % MOD * invfact[n - r] % MOD);
    }

    public long pow(long i, long n, long MOD = 1000000007)
    {
        long res = 1;
        while (n > 0)
        {
            if ((n & 1) != 0) res = res * i % MOD;
            i = i * i % MOD;
            n >>= 1;
        }
        return res;
    }

    public long gcd(long i, long y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public long lcm(long i, long y)
    {
        return i * y / gcd(i, y);
    }

    public Dictionary<long, int> primefactorization(long N)
    {
        var ret = new Dictionary<long, int>();

        for (long i = 2; i * i <= N; ++i)
        {
            int cnt = 0;
            while (N % i == 0)
            {
                cnt++;
                N /= i;
            }
            if (cnt != 0) ret[i] = cnt;

        }
        if (N >= 2) ret[N] = 1;

        return ret;

    }

    public List<long> divisorenumrate(long N)
    {
        var ret = new List<long>();

        for (long i = 1; i * i <= N; ++i)
        {

            if (N % i == 0)
            {
                ret.Add(i);
                ret.Add(N / i);
            }

        }

        return ret;

    }

    public void swap<T>(ref T a, ref T b)
    {
        var i = a;
        a = b;
        b = i;
    }

}

class myinout
{
    //in
    public string str => Console.ReadLine();

    public string[] strm => str.Split(' ');

    public long[] longm => strm.Select(long.Parse).ToArray();
    public int[] intm => strm.Select(int.Parse).ToArray();
    public char[] charm => str.ToArray();
    public double[] doublem => strm.Select(double.Parse).ToArray();
    public long long1 => longm[0];
    public int int1 => intm[0];
    public char char1 => charm[0];
    public double double1 => doublem[0];



    //out
    public void wri<T>(T i)
    {
        Console.WriteLine(i);
    }
    public void wri<T>(T[] i)
    {
        Console.WriteLine(string.Join(" ", i));
    }

    public void yn(bool i)
    {
        if (i) wri("Yes");
        else wri("No");
    }

}


Measured memory_bytes: 106844