from collections import deque

n = int(input())
graph_W = []
for _ in range(n):
    graph_W.append(list(map(int, input().split())))
    
num_root = [0]*n
work_queue = deque([])
visited = set()

work_queue.append(1)
visited.add(1)
cnt = 0
while work_queue:
    here = work_queue.popleft() - 1
    num_node = graph_W[here][1]
    cnt += 1
    if num_node == 0:
        continue
    for k in range(num_node):
        num_graph = graph_W[here][k+2]
        if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1


print(1,0)
for i in range(1,n):
    if num_root[i] != 0:
        print(i+1,num_root[i])
    else:
        print(i+1,-1) from collections import deque n = int(input()) n int(input()) int input() input graph_W = [] graph_W [] for _ in range(n):
    graph_W.append(list(map(int, input().split())))
     _ range(n) range n graph_W.append(list(map(int, input().split()))) graph_W.append graph_W append list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split num_root = [0]*n num_root [0]*n [0] 0 n work_queue = deque([]) work_queue deque([]) deque [] visited = set() visited set() set work_queue.append(1) work_queue.append work_queue append 1 visited.add(1) visited.add visited add 1 cnt = 0 cnt 0 while work_queue:
    here = work_queue.popleft() - 1
    num_node = graph_W[here][1]
    cnt += 1
    if num_node == 0:
        continue
    for k in range(num_node):
        num_graph = graph_W[here][k+2]
        if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1 work_queue here = work_queue.popleft() - 1 here work_queue.popleft() - 1 work_queue.popleft() work_queue.popleft work_queue popleft 1 num_node = graph_W[here][1] num_node graph_W[here][1] [here] graph_W here 1 cnt += 1 cnt 1 if num_node == 0:
        continue
     num_node == 0 num_node 0 continue for k in range(num_node):
        num_graph = graph_W[here][k+2]
        if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1 k range(num_node) range num_node num_graph = graph_W[here][k+2] num_graph graph_W[here][k+2] [here] graph_W here k+2 k 2 if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1 num_graph not in visited num_graph visited work_queue.append(num_graph) work_queue.append work_queue append num_graph visited.add(num_graph) visited.add visited add num_graph num_root[num_graph-1] = num_root[here] + 1 num_root[num_graph-1] num_root num_graph-1 num_graph 1 num_root[here] + 1 num_root[here] num_root here 1 print(1,0) print 1 0 for i in range(1,n):
    if num_root[i] != 0:
        print(i+1,num_root[i])
    else:
        print(i+1,-1) i range(1,n) range 1 n if num_root[i] != 0:
        print(i+1,num_root[i])
    else:
        print(i+1,-1) num_root[i] != 0 num_root[i] num_root i 0 print(i+1,num_root[i]) print i+1 i 1 num_root[i] num_root i print(i+1,-1) print i+1 i 1 -1 here = work_queue.popleft() - 1 work_queue.popleft() - 1 here num_graph = graph_W[here][k+2] graph_W[here][k+2] num_graph cnt += 1 1 cnt n = int(input()) int(input()) n num_root = [0]*n [0]*n num_root cnt = 0 0 cnt num_node = graph_W[here][1] graph_W[here][1] num_node work_queue = deque([]) deque([]) work_queue visited = set() set() visited graph_W = [] [] graph_W
from collections import deque

n = int(input())
graph_W = []
for _ in range(n):
    graph_W.append(list(map(int, input().split())))
    
num_root = [0]*n
work_queue = deque([])
visited = set()

work_queue.append(1)
visited.add(1)
cnt = 0
while work_queue:
    here = work_queue.popleft() - 1
    num_node = graph_W[here][1]
    cnt += 1
    if num_node == 0:
        continue
    for k in range(num_node):
        num_graph = graph_W[here][k+2]
        if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1


print(1,0)
for i in range(1,n):
    if num_root[i] != 0:
        print(i+1,num_root[i])
    else:
        print(i+1,-1)
