using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static System.Math;

namespace Sample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var sc = new SetConsole()) { Solve(); }
        }

        public static void Solve()
        {
            var n = GetString().ToInt();
            var list = new List<List<int>>();
            for (int i = 0; i < n; i++) { list.Add(new List<int>(Enumerable.Repeat(0, n - 1))); }
            for (int i = 0; i < n; i++)
            {
                var a = GetArray<int>();
                for (int j = 0; j < a.Length; j++)
                {
                    list[i][j] = a[j] - 1;
                }
            }
            var q = new List<Tuple<int, int>>();
            Func<int, bool> check = player =>
            {
                if (!list[player].Any()) { return false; }

                int matchedPlayer = list[player].First();
                if (!list[matchedPlayer].Any()) { return false; }

                if (list[matchedPlayer].First() != player) { return false; }

                var pair = Tuple.Create(player, matchedPlayer);
                if (pair.Item2 < pair.Item1) { pair = Tuple.Create(pair.Item2, pair.Item1); }
                q.Add(pair);
                return true;
            };

            for (int i = 0; i < n; i++) { check(i); }

            var day = 0;
            while (q.Any())
            {
                day++;
                var prevQ = new List<Tuple<int, int>>();
                foreach (var item in q.Distinct())
                {
                    prevQ.Add(item);
                }
                q = new List<Tuple<int, int>>();
                foreach (var p in prevQ)
                {
                    // 試合を消す
                    var i = p.Item1;
                    var j = p.Item2;
                    list[i].RemoveAt(0);
                    list[j].RemoveAt(0);
                }
                foreach (var p in prevQ)
                {
                    check(p.Item1);
                    check(p.Item2);
                }
            }

            foreach (var item in list)
            {
                if (item.Any()) { CWrite(-1); return; }
            }
            CWrite(day);
        }

        static void CWrite<T>(T str) => Console.WriteLine(str);
        static string GetString() => Console.ReadLine();
        static T[] GetArray<T>()
        {
            var str = Console.ReadLine();
            if (str == null) { throw new ArgumentNullException("標準入力がnullです 解答がおかしいはずです"); }

            var t = typeof(T);
            if (t == typeof(string))
            {
                return (T[])(object)str.Split();
            }
            if (t == typeof(int))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            if (t == typeof(long))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            if (t == typeof(double))
            {
                return (T[])(object)str.ToNumArray<T>();
            }
            throw new NotSupportedException($"{t} is not supported.");
        }
    }



    class SetConsole : IDisposable
    {
        readonly StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
        public SetConsole()
        {
            sw.AutoFlush = false;
            Console.SetOut(sw);
        }
        public void Dispose()
        {
            Console.Out.Flush();
            sw.AutoFlush = true;
            Console.SetOut(sw);
        }
    }

    static class ExtentionsLibrary
    {
        public static T[] CopyArray<T>(this T[] array)
        {
            var newArray = new T[array.Length];
            Array.Copy(array, newArray, array.Length);
            return newArray;
        }
        public static T[,] CopyArray<T>(this T[,] array)
        {
            var firstDimentionLength = array.GetLength(0);
            var secondDimentionLength = array.GetLength(1);
            var newDArray = new T[firstDimentionLength, secondDimentionLength];
            Array.Copy(array, newDArray, firstDimentionLength * secondDimentionLength);
            return newDArray;
        }
        public static T[] ToNumArray<T>(this string str)
        {
            var t = typeof(T);
            if (t == typeof(int))
            {
                return (T[])(object)str.Split().Select(int.Parse).ToArray();
            }
            if (t == typeof(long))
            {
                return (T[])(object)str.Split().Select(long.Parse).ToArray();
            }
            if (t == typeof(double))
            {
                return (T[])(object)str.Split().Select(double.Parse).ToArray();
            }
            throw new NotSupportedException();
        }
        public static int ToInt(this string str) => int.Parse(str);
        public static int ToInt(this char chr) => int.Parse(chr.ToString());
        public static long ToLong(this string str) => long.Parse(str);
        public static DateTime ToDateTime(this string str) => DateTime.Parse(str);
    }
}


Measured memory_bytes: 43792