import queue

n = int(input())
graph = []
for _ in range(n):
    line = list(map(int, input().split()))
    graph.append([v - 1 for v in line[2:]])

q = queue.Queue()
q.put(0)
dist = [-1] * n
dist[0] = 0

while True:
    v = q.get()
    for next_v in graph[v]:
        if dist[next_v] == -1: 
            q.put(next_v)
            dist[next_v] = dist[v] + 1

    if q.empty():
        break

for i in range(n):
    print(i + 1, dist[i])
 import queue queue n = int(input()) n int(input()) int input() input graph = [] graph [] for _ in range(n):
    line = list(map(int, input().split()))
    graph.append([v - 1 for v in line[2:]]) _ range(n) range n line = list(map(int, input().split())) line list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split v - 1 for v in line[2:] for v in line[2:] v line[2:] line 2: 2 for v in line[2:] v - 1 v 1 graph.append([v - 1 for v in line[2:]]) graph.append graph append [v - 1 for v in line[2:]] q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(0) q.put q put 0 dist = [-1] * n dist [-1] * n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 while True:
    v = q.get()
    for next_v in graph[v]:
        if dist[next_v] == -1: 
            q.put(next_v)
            dist[next_v] = dist[v] + 1

    if q.empty():
        break True v = q.get() v q.get() q.get q get for next_v in graph[v]:
        if dist[next_v] == -1: 
            q.put(next_v)
            dist[next_v] = dist[v] + 1

     next_v graph[v] graph v if dist[next_v] == -1: 
            q.put(next_v)
            dist[next_v] = dist[v] + 1

     dist[next_v] == -1 dist[next_v] dist next_v -1 q.put(next_v) q.put q put next_v dist[next_v] = dist[v] + 1 dist[next_v] dist next_v dist[v] + 1 dist[v] dist v 1 if q.empty():
        break q.empty() q.empty q empty break for i in range(n):
    print(i + 1, dist[i]) i range(n) range n print(i + 1, dist[i]) print i + 1 i 1 dist[i] dist i n = int(input()) int(input()) n dist = [-1] * n [-1] * n dist q = queue.Queue() queue.Queue() q graph = [] [] graph line = list(map(int, input().split())) list(map(int, input().split())) line v = q.get() q.get() v
import queue

n = int(input())
graph = []
for _ in range(n):
    line = list(map(int, input().split()))
    graph.append([v - 1 for v in line[2:]])

q = queue.Queue()
q.put(0)
dist = [-1] * n
dist[0] = 0

while True:
    v = q.get()
    for next_v in graph[v]:
        if dist[next_v] == -1: 
            q.put(next_v)
            dist[next_v] = dist[v] + 1

    if q.empty():
        break

for i in range(n):
    print(i + 1, dist[i])

