from collections import deque

n = int(input())
graphs = [[] for i in range(n+1)]
visited = [False] * (n+1)
min_s = [-1] * (n+1)

for i in range(1, n+1):
    nodes = list(map(int, input().split()))
    graphs[i].extend(nodes[1:])

q = deque()
q.append([1, 0])
while q:
    node, j = q.popleft()
    if not visited[node]:
        visited[node] = True
        min_s[node] = j
    nodes = graphs[node]
    for node in nodes:
        if not visited[node]:
            q.append([node, j+1])

for i in range(1, n+1):
    print(i, min_s[i]) from collections import deque n = int(input()) n int(input()) int input() input [] for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) [] graphs = [[] for i in range(n+1)] graphs [[] for i in range(n+1)] visited = [False] * (n+1) visited [False] * (n+1) [False] False n+1 n 1 min_s = [-1] * (n+1) min_s [-1] * (n+1) [-1] -1 n+1 n 1 for i in range(1, n+1):
    nodes = list(map(int, input().split()))
    graphs[i].extend(nodes[1:]) i range(1, n+1) range 1 n+1 n 1 nodes = list(map(int, input().split())) nodes list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split graphs[i].extend(nodes[1:]) [i].extend [i] graphs i extend nodes[1:] nodes 1: 1 q = deque() q deque() deque q.append([1, 0]) q.append q append [1, 0] 1 0 while q:
    node, j = q.popleft()
    if not visited[node]:
        visited[node] = True
        min_s[node] = j
    nodes = graphs[node]
    for node in nodes:
        if not visited[node]:
            q.append([node, j+1]) q node, j = q.popleft() node q.popleft() q.popleft q popleft j if not visited[node]:
        visited[node] = True
        min_s[node] = j
     not visited[node] visited[node] visited node visited[node] = True visited[node] visited node True min_s[node] = j min_s[node] min_s node j nodes = graphs[node] nodes graphs[node] graphs node for node in nodes:
        if not visited[node]:
            q.append([node, j+1]) node nodes if not visited[node]:
            q.append([node, j+1]) not visited[node] visited[node] visited node q.append([node, j+1]) q.append q append [node, j+1] node j+1 j 1 for i in range(1, n+1):
    print(i, min_s[i]) i range(1, n+1) range 1 n+1 n 1 print(i, min_s[i]) print i min_s[i] min_s i graphs = [[] for i in range(n+1)] [[] for i in range(n+1)] graphs j = q.popleft() q.popleft() j node, j = q.popleft() q.popleft() node q = deque() deque() q n = int(input()) int(input()) n visited = [False] * (n+1) [False] * (n+1) visited min_s = [-1] * (n+1) [-1] * (n+1) min_s nodes = list(map(int, input().split())) list(map(int, input().split())) nodes nodes = graphs[node] graphs[node] nodes
from collections import deque

n = int(input())
graphs = [[] for i in range(n+1)]
visited = [False] * (n+1)
min_s = [-1] * (n+1)

for i in range(1, n+1):
    nodes = list(map(int, input().split()))
    graphs[i].extend(nodes[1:])

q = deque()
q.append([1, 0])
while q:
    node, j = q.popleft()
    if not visited[node]:
        visited[node] = True
        min_s[node] = j
    nodes = graphs[node]
    for node in nodes:
        if not visited[node]:
            q.append([node, j+1])

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