from collections import deque

N = int(input())

nodes = [tuple(map(int, input().split(' '))) for _ in range(N)]

distances = [-1] * (N + 1)

queue = deque()
queue.append((1, 0))

while len(queue) > 0:
    node, distance = queue.popleft()
    if distances[node] != -1:
        continue
    distances[node] = distance

    neighbors = nodes[node - 1][2:]
    for neighbor in neighbors:
        queue.append((neighbor, distance + 1))

for node in range(1, N + 1):
    print(node, distances[node])
 from collections import deque N = int(input()) N int(input()) int input() input tuple(map(int, input().split(' '))) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) tuple(map(int, input().split(' '))) tuple map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' nodes = [tuple(map(int, input().split(' '))) for _ in range(N)] nodes [tuple(map(int, input().split(' '))) for _ in range(N)] distances = [-1] * (N + 1) distances [-1] * (N + 1) [-1] -1 N + 1 N 1 queue = deque() queue deque() deque queue.append((1, 0)) queue.append queue append (1, 0) 1 0 while len(queue) > 0:
    node, distance = queue.popleft()
    if distances[node] != -1:
        continue
    distances[node] = distance

    neighbors = nodes[node - 1][2:]
    for neighbor in neighbors:
        queue.append((neighbor, distance + 1)) len(queue) > 0 len(queue) len queue 0 node, distance = queue.popleft() node queue.popleft() queue.popleft queue popleft distance if distances[node] != -1:
        continue
     distances[node] != -1 distances[node] distances node -1 continue distances[node] = distance distances[node] distances node distance neighbors = nodes[node - 1][2:] neighbors nodes[node - 1][2:] [node - 1] nodes node - 1 node 1 2: 2 for neighbor in neighbors:
        queue.append((neighbor, distance + 1)) neighbor neighbors queue.append((neighbor, distance + 1)) queue.append queue append (neighbor, distance + 1) neighbor distance + 1 distance 1 for node in range(1, N + 1):
    print(node, distances[node]) node range(1, N + 1) range 1 N + 1 N 1 print(node, distances[node]) print node distances[node] distances node nodes = [tuple(map(int, input().split(' '))) for _ in range(N)] [tuple(map(int, input().split(' '))) for _ in range(N)] nodes neighbors = nodes[node - 1][2:] nodes[node - 1][2:] neighbors distance = queue.popleft() queue.popleft() distance node, distance = queue.popleft() queue.popleft() node N = int(input()) int(input()) N distances = [-1] * (N + 1) [-1] * (N + 1) distances queue = deque() deque() queue
from collections import deque

N = int(input())

nodes = [tuple(map(int, input().split(' '))) for _ in range(N)]

distances = [-1] * (N + 1)

queue = deque()
queue.append((1, 0))

while len(queue) > 0:
    node, distance = queue.popleft()
    if distances[node] != -1:
        continue
    distances[node] = distance

    neighbors = nodes[node - 1][2:]
    for neighbor in neighbors:
        queue.append((neighbor, distance + 1))

for node in range(1, N + 1):
    print(node, distances[node])

