fun main(args: Array<String>) {
    Main.init()

    Main.execute()

    Main.display()
}

data class Timestamp(var findedAt: Int? = null, var endedAt: Int? = null, var distance: Int? = null)

class Main {

    companion object {
        private val graphMatrix = arrayListOf<ArrayList<Boolean>>()

        private val timestampList = arrayListOf<Timestamp>()

        val queue = Queue(arrayListOf())

        private var currentTime = 0

        fun init() {
            val num = readLine()!!.toInt()

            initgraph(num)

            for (index in 1..num) {
                val inputs = readLine()!!.split(" ")
                val nodeIndex = inputs[0].toInt()
                for (inputIndex in 2 until inputs.size) {

                    val touchedNode = inputs[inputIndex].toInt()
                    graphMatrix[nodeIndex - 1][touchedNode - 1] = true
                }
            }
        }

        fun display() {
            timestampList.forEachIndexed { index, it ->
                println("${index + 1} ${it.distance ?: -1}")
            }
        }

        /**
         * 幅優先
         */
        fun execute() {
            timestampList[0].distance = 0
            gather(1)
        }

        /**
         * 集める
         */
        fun gather(index: Int) {

            val currentDistance = timestampList[index - 1].distance!!

            graphMatrix[index - 1].forEachIndexed { i, canGo ->

                if (!canGo) return@forEachIndexed

                // 未探索かつキューにまだなかったら
                if (timestampList[i].distance == null && !queue.contains(i)) {
                    queue.enqueue(i)

                    // 距離を記録
                    timestampList[i].distance = currentDistance + 1
                }
            }

            val topIndex = queue.dequeue() ?: return

            // 一つ移動
            gather(topIndex + 1)
        }

        fun initgraph(num: Int) {
            for (i in 0 until num) {
                graphMatrix.add(arrayListOf())
                for (j in 0 until num) {
                    graphMatrix[i].add(false)
                }
                timestampList.add(Timestamp())
            }
        }
    }
}


class Queue(list: MutableList<Int>) {

    var items: MutableList<Int> = list

    fun isEmpty(): Boolean = items.isEmpty()


    fun contains(i: Int): Boolean = items.contains(i)

    fun size(): Int = items.count()

    override fun toString() = items.toString()

    fun enqueue(element: Int) {
        items.add(element)
    }

    fun dequeue(): Int? {
        if (this.isEmpty()) {
            return null
        } else {
            return items.removeAt(0)
        }
    }

    fun peek(): Int? {
        return items[0]
    }
}
