module app;

import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;

void main()
{
	auto input = parseInput();
    auto result = main2(input);	
	writefln("%d", result);
}

int main2(Input input)
{
    struct Player
    {
        bool done;
        int[] rest;
    }

    Player[] players;
    players.length = input.n + 1;
    for (int i = 0; i < input.a.length; i++)
        players[i+1].rest = input.a[i];

    int[] cache;
    foreach (i; 1..input.n+1)
        cache ~= i;

    int days = 0;
    while (true)
    {
        int[] lastDay = cache;
        cache.length = 0;
        days++;

        bool found = false;

        void tryMatch(int i)
        {
            if (players[i].done)
                return;

            if (players[i].rest.length == 0)
                return;

            auto nextOpponent = players[i].rest[0];
            if (!players[nextOpponent].done && players[nextOpponent].rest[0] == i)
            {
                players[nextOpponent].done = true;
                players[nextOpponent].rest = players[nextOpponent].rest[1..$];
                players[i].done = true;
                players[i].rest = players[i].rest[1..$];

                cache ~= [i, nextOpponent];
            }
        }

        foreach (i; lastDay)
        {
            tryMatch(i);
        }

        if (cache.length == 0)
            return -1;

        bool playerExists;

        for (int i = 1; i < players.length; i++)
        {
            if (players[i].rest.length != 0)
            {
                players[i].done = false;
                playerExists = true;
            }
        }

        if (!playerExists)
            return days;
    }
}


unittest
{
    Input input = {3,[[2,3],[1,3],[1,2]]};
    auto result = main2(input);
    assert(result == 3);
}
unittest
{
    Input input = {4,[[2,3,4],[1,3,4],[4,1,2],[3,1,2]]};
    auto result = main2(input);
    assert(result == 4);
}
unittest
{
    Input input = {3,[[2,3],[3,1],[1,2]]};
    auto result = main2(input);
    assert(result == -1);
}


struct Input
{
	int n;
    int[][] a;
}

Input parseInput()
{
	Input input;
	input.n = readln().strip().to!int;
    input.a = new int[][input.n];
    for (int i = 0; i < input.n; i++)
    {
    	input.a[i] = readln().strip().split().map!(a => a.to!int).array;
    }
	return input;
}
