data class Node(val id: Int, val edges: List<Int>, var dest: Int = -1, var visited: Boolean = false)

fun main(args: Array<String>) {
    val nodeList = List(readLine()?.toInt() ?: 0) {
        val input = readLine()?.split(" ".toRegex())?.map(String::toInt) ?: listOf()
        Node(input[0], input.subList(2, 2 + input[1]))
    }
    Bfs(nodeList).print()
}

class Bfs(private val nodeList: List<Node>) {
    init {
        bfs()
    }

    fun print() {
        println(
            nodeList.joinToString("\n") {
                "${it.id} ${it.dest}"
            }
        )
    }

    private fun bfs() {
        var dest = 0
        var sameTimeNodes = listOf(nodeList.first())
        while (sameTimeNodes.isNotEmpty()) {
            val nodes = arrayListOf<Node>()
            sameTimeNodes.forEach { node ->
                if (node.dest == -1 || node.dest > dest) {
                    node.dest = dest
                }
                nodes.addAll(nodeList.filter { !it.visited && node.edges.contains(it.id) })
            }
            sameTimeNodes.forEach {
                it.visited = true
            }
            sameTimeNodes = nodes
            dest++
        }
    }
}
