import queue

n = int(input())
v = [[]]
for _ in range(n):
    edges = sorted([int(x) for x in input().split()][2:])
    v.append(edges)
answers = [[idx, -1] for idx in range(n + 1)]
q = queue.Queue()
q.put(1)

answers[1][1] = 0
while not q.empty():
    node = q.get()
    for next_node in v[node]:
        if answers[next_node][1] != -1:
            continue
        q.put(next_node)
        answers[next_node][1] = answers[node][1] + 1

for idx, time in answers[1:]:
    print(idx, time)
 import queue queue n = int(input()) n int(input()) int input() input v = [[]] v [[]] [] for _ in range(n):
    edges = sorted([int(x) for x in input().split()][2:])
    v.append(edges) _ range(n) range n int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x edges = sorted([int(x) for x in input().split()][2:]) edges sorted([int(x) for x in input().split()][2:]) sorted [int(x) for x in input().split()][2:] [int(x) for x in input().split()] 2: 2 v.append(edges) v.append v append edges [idx, -1] for idx in range(n + 1) for idx in range(n + 1) idx range(n + 1) range n + 1 n 1 for idx in range(n + 1) [idx, -1] idx -1 answers = [[idx, -1] for idx in range(n + 1)] answers [[idx, -1] for idx in range(n + 1)] q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(1) q.put q put 1 answers[1][1] = 0 answers[1][1] [1] answers 1 1 0 while not q.empty():
    node = q.get()
    for next_node in v[node]:
        if answers[next_node][1] != -1:
            continue
        q.put(next_node)
        answers[next_node][1] = answers[node][1] + 1 not q.empty() q.empty() q.empty q empty node = q.get() node q.get() q.get q get for next_node in v[node]:
        if answers[next_node][1] != -1:
            continue
        q.put(next_node)
        answers[next_node][1] = answers[node][1] + 1 next_node v[node] v node if answers[next_node][1] != -1:
            continue
         answers[next_node][1] != -1 answers[next_node][1] [next_node] answers next_node 1 -1 continue q.put(next_node) q.put q put next_node answers[next_node][1] = answers[node][1] + 1 answers[next_node][1] [next_node] answers next_node 1 answers[node][1] + 1 answers[node][1] [node] answers node 1 1 for idx, time in answers[1:]:
    print(idx, time) idx time answers[1:] answers 1: 1 print(idx, time) print idx time v = [[]] [[]] v n = int(input()) int(input()) n node = q.get() q.get() node edges = sorted([int(x) for x in input().split()][2:]) sorted([int(x) for x in input().split()][2:]) edges q = queue.Queue() queue.Queue() q answers = [[idx, -1] for idx in range(n + 1)] [[idx, -1] for idx in range(n + 1)] answers
import queue

n = int(input())
v = [[]]
for _ in range(n):
    edges = sorted([int(x) for x in input().split()][2:])
    v.append(edges)
answers = [[idx, -1] for idx in range(n + 1)]
q = queue.Queue()
q.put(1)

answers[1][1] = 0
while not q.empty():
    node = q.get()
    for next_node in v[node]:
        if answers[next_node][1] != -1:
            continue
        q.put(next_node)
        answers[next_node][1] = answers[node][1] + 1

for idx, time in answers[1:]:
    print(idx, time)

