using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC139.E();
        }


        class ABC139
        {
            public static void A()
            {
                var S = Read.Str();
                var T = Read.Str();
                Console.WriteLine(Enumerable.Range(0, 3).Count(i => S[i] == T[i]));
            }
            public static void B()
            {
                var A = Read.Int();
                var B = Read.Int();
                Console.WriteLine((B - 1 + A - 2) / (A - 1));
            }
            public static void C()
            {
                var N = Read.Int();
                var H = Read.IntN();
                int max = 0;
                int current = 0;
                for(int i = 0; i < N - 1; ++i)
                {
                    if (H[i] >= H[i + 1])
                    {
                        current++;
                        max = Math.Max(max, current);
                    }
                    else
                    {
                        current = 0;
                    }
                }
                Console.WriteLine(max);
            }
            public static void D()
            {
                var N = Read.Long();
                Console.WriteLine(N * (N - 1) / 2);
            }
            public static void E()
            {
                var N = Read.Int();
                Func<int, int, int> key = (i, j) => i > j ? j * N + i : i * N + j;
                Dictionary<int, HashSet<int>> Adj = new Dictionary<int, HashSet<int>>();
                var degreeIn = new int[N * N];
                for(int i = 0; i < N; ++i)
                {
                    for(int j = i + 1; j < N; ++j)
                    {
                        var k = key(i, j);
                        Adj.Add(k, new HashSet<int>());
                    }
                }
                for(int i = 0; i < N; ++i)
                {
                    var A = Read.IntN();
                    for(int k = 0; k < N - 2; ++k)
                    {
                        var k1 = key(i, A[k] - 1);
                        var k2 = key(i, A[k + 1] - 1);
                        Adj[k1].Add(k2);
                        degreeIn[k2]++;
                    }
                }
                var starts = new List<int>();
                for (int i = 0; i < N; ++i)
                {
                    for (int j = i + 1; j < N; ++j)
                    {
                        var k = key(i, j);
                        if (degreeIn[k] == 0)
                        {
                            starts.Add(k);
                        }
                    }
                }
                if (starts.Count == 0) { Console.WriteLine(-1); return; }
                var done1 = new bool[N * N];
                var done2 = new bool[N * N];
                var history = new Stack<int>();
                var top = Util.TopologicalSort(Adj, starts, done1, done2, history);
                if (!top) { Console.WriteLine(-1); return; }
                var dp = new int[N * N];
                while (history.Count > 0)
                {
                    var v = history.Pop();
                    foreach(var w in Adj[v])
                    {
                        dp[w] = Math.Max(dp[w], dp[v] + 1);
                    }
                }
                Console.WriteLine(dp.Max() + 1);
            }
            public static void F()
            {

            }
        }

        class Util
        {
            public static bool TopologicalSort2(Dictionary<int, HashSet<int>> adj, int v, bool[] done1, bool[] done2, Stack<int> history)
            {
                done1[v] = true;
                foreach(var w in adj[v])
                {
                    if (done2[w]) { continue; }
                    if (done1[w] && !done2[w])
                    {
                        return false;
                    }
                    if (!TopologicalSort2(adj, w, done1, done2, history)) { return false; }
                }
                history.Push(v);
                done2[v] = true;
                return true;
            }
            public static bool TopologicalSort(Dictionary<int, HashSet<int>> adj, List<int> starts, bool[] done1, bool[] done2, Stack<int> history)
            {
                var stack = new Stack<Tuple<int, bool>>();
                foreach (var s in starts)
                {
                    stack.Push(new Tuple<int, bool>(s, false));
                }
                while (stack.Count > 0)
                {
                    var t = stack.Pop();
                    var v = t.Item1;
                    if (t.Item2)
                    {
                        history.Push(v);
                        done2[v] = true;
                        continue;
                    }
                    done1[v] = true;
                    stack.Push(new Tuple<int, bool>(v, true));
                    foreach (var w in adj[v])
                    {
                        if (done2[w]) { continue; }
                        if (done1[w] && !done2[w])
                        {
                            return false;
                        }
                        stack.Push(new Tuple<int, bool>(w, false));
                    }
                }
                return true;
            }
            static bool LoopDetect(Dictionary<int, HashSet<int>> adj, int v, bool[] done1, bool[] done2, Stack<int> history)
            {
                done1[v] = true;
                history.Push(v);
                foreach (var w in adj[v])
                {
                    if (done2[w]) { continue; }
                    if (done1[w] && !done2[w])
                    {
                        history.Push(w);
                        return true;
                    }
                    if (LoopDetect(adj, w, done1, done2, history))
                    {
                        return true;
                    }
                }
                history.Pop();
                done2[v] = true;
                return false;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN(); dict[ab[0]].Add(ab[1]);
                    if (!isDirectional) { dict[ab[1]].Add(ab[0]); }
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}


Measured memory_bytes: 167952