from collections import deque

n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0]
    num = input_list[1]

    if num >= 1:
        nodes = input_list[2:]
        for node in nodes:
            graph[index].append(node)
            # graph[node].append(index)

visited = [-1] * (n + 1)
num = [-1] * (n + 1)
q = deque()
visited[1] = 1
num[1] = 0
q.append(1)

while q:
    index = q.popleft()
    
    for node in graph[index]:
        if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node)


for i, j in enumerate(num):
    if i == 0:
        continue
    print(i, j)


 from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [] graph = [[] for _ in range(n + 1)] graph [[] for _ in range(n + 1)] for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0]
    num = input_list[1]

    if num >= 1:
        nodes = input_list[2:]
        for node in nodes:
            graph[index].append(node)
            # graph[node].append(index) _ range(n) range n input_list = list(map(int, input().split())) input_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split index = input_list[0] index input_list[0] input_list 0 num = input_list[1] num input_list[1] input_list 1 if num >= 1:
        nodes = input_list[2:]
        for node in nodes:
            graph[index].append(node)
            # graph[node].append(index) num >= 1 num 1 nodes = input_list[2:] nodes input_list[2:] input_list 2: 2 for node in nodes:
            graph[index].append(node)
            # graph[node].append(index) node nodes graph[index].append(node) [index].append [index] graph index append node visited = [-1] * (n + 1) visited [-1] * (n + 1) [-1] -1 n + 1 n 1 num = [-1] * (n + 1) num [-1] * (n + 1) [-1] -1 n + 1 n 1 q = deque() q deque() deque visited[1] = 1 visited[1] visited 1 1 num[1] = 0 num[1] num 1 0 q.append(1) q.append q append 1 while q:
    index = q.popleft()
    
    for node in graph[index]:
        if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node) q index = q.popleft() index q.popleft() q.popleft q popleft for node in graph[index]:
        if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node) node graph[index] graph index if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node) visited[node] == -1 visited[node] visited node -1 visited[node] = 1 visited[node] visited node 1 num[node] = num[index] + 1 num[node] num node num[index] + 1 num[index] num index 1 q.append(node) q.append q append node for i, j in enumerate(num):
    if i == 0:
        continue
    print(i, j) i j enumerate(num) enumerate num if i == 0:
        continue
     i == 0 i 0 continue print(i, j) print i j num = input_list[1] input_list[1] num index = input_list[0] input_list[0] index visited = [-1] * (n + 1) [-1] * (n + 1) visited num = [-1] * (n + 1) [-1] * (n + 1) num n = int(input()) int(input()) n graph = [[] for _ in range(n + 1)] [[] for _ in range(n + 1)] graph index = q.popleft() q.popleft() index q = deque() deque() q nodes = input_list[2:] input_list[2:] nodes input_list = list(map(int, input().split())) list(map(int, input().split())) input_list
from collections import deque

n = int(input())
graph = [[] for _ in range(n + 1)]
for _ in range(n):
    input_list = list(map(int, input().split()))
    index = input_list[0]
    num = input_list[1]

    if num >= 1:
        nodes = input_list[2:]
        for node in nodes:
            graph[index].append(node)
            # graph[node].append(index)

visited = [-1] * (n + 1)
num = [-1] * (n + 1)
q = deque()
visited[1] = 1
num[1] = 0
q.append(1)

while q:
    index = q.popleft()
    
    for node in graph[index]:
        if visited[node] == -1:
            visited[node] = 1
            num[node] = num[index] + 1
            q.append(node)


for i, j in enumerate(num):
    if i == 0:
        continue
    print(i, j)



