import java.util.*

fun main(args: Array<String>) = p28()

fun p28() {
    data class Vertex(val neighbor: MutableSet<Int> = mutableSetOf())

    val n = readLine()!!.toInt()
    val vertexes = Array(n) {
        val line = readLine()!!.split(' ').map { it.toInt() }
        Vertex(line.drop(2).map { it - 1 }.toMutableSet())
    }

    val queue = ArrayDeque(listOf(0 to 0))
    val distances = IntArray(n) { -1 }
    val visited = mutableSetOf(0)

    while (queue.isNotEmpty()) {
        val (index, dist) = queue.poll()
        distances[index] = dist
        vertexes[index].neighbor.filter { visited.add(it) }
            .map { it to dist + 1 }.let { queue.addAll(it) }
    }

    val answer = distances.mapIndexed { index, dist -> "${index + 1} $dist" }
        .joinToString(System.lineSeparator())

    println(answer)
}

