using CompProgLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ALDS1_11_C
{
    public class Program

    {
        public static void Main(string[] args)
        {
            int n = ReadInt();
            Graph gr = new Graph();

            for (int i = 0 ; i < n ; i++)
            {
                int[] line = ReadIntAr();
                gr.SetGraph(line[0], line.Skip(2).ToArray());
            }

            gr.BFS(1);

            foreach (var item in gr.Adj)
            {
                Console.WriteLine("{0} {1}", item.Key, item.Value.Depth);
            }
        }

        static string ReadSt() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Console.ReadLine()); }
        static long ReadLong() { return long.Parse(Console.ReadLine()); }
        static double ReadDouble() { return double.Parse(Console.ReadLine()); }
        static string[] ReadStAr(char sep = ' ') { return Console.ReadLine().Split(sep); }
        static int[] ReadIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => int.Parse(e)); }
        static long[] ReadLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => long.Parse(e)); }
        static double[] ReadDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Split(sep), e => double.Parse(e)); }

    }

}

namespace CompProgLib
{
    public class Graph
    {
        public class Node
        {
            public List<int> Vertex { get; internal set; }
            public int D { get; internal set; }
            public int F { get; internal set; }
            public bool IsVisited { get; internal set; }
            public int Depth { get; internal set; }

            public Node(List<int> v)
            {
                Vertex = v;
                Depth = -1;
            }
        }

        public Dictionary<int, Node> Adj { get; set; }

        public Graph()
        {
            Adj = new Dictionary<int, Node>();
        }

        /// <summary>
        /// 隣接行列の指定したインデックスに値をセットする
        /// </summary>
        /// <param name="index">頂点u</param>
        /// <param name="item">頂点uに接続する頂点</param>
        public void SetGraph(int index, int[] item)
        {
            Adj.Add(index, new Node(item.ToList()));
        }

        /// <summary>
        /// 行列を0で初期化する
        /// </summary>
        public void Clear() { Adj.Clear(); }

        /// <summary>
        /// 深さ優先探索
        /// </summary>
        private int Time = 0;
        public void DFS(int v)
        {
            Adj[v].D = ++Time;
            Adj[v].IsVisited = true;
            foreach (int vertex in Adj[v].Vertex)
            {
                if (!Adj[vertex].IsVisited) DFS(vertex);
            }
            Adj[v].F = ++Time;
        }

        /// <summary>
        /// 幅優先探索
        /// </summary>
        /// <param name="v"></param>
        public void BFS(int v)
        {

            Queue<Node> que = new Queue<Node>();

            Adj[v].Depth = 0;
            Adj[v].IsVisited = true;
            que.Enqueue(Adj[v]);

            while (que.Count() != 0)
            {
                Node node = que.Dequeue();
                foreach (int vertex in node.Vertex)
                {
                    if (!Adj[vertex].IsVisited)
                    {
                        Adj[vertex].IsVisited = true;
                        Adj[vertex].Depth = node.Depth + 1;
                        que.Enqueue(Adj[vertex]);
                        
                    }
                }
            }

        }
    }
}


Measured memory_bytes: 18824