from collections import deque
from sys import stdin
input = stdin.readline

N = int(input())
neighborlist = [None for _ in range(N+1)]
for _ in range(1, N+1):
    node, _, *L = list(map(int, input().split()))
    neighborlist[node] = L

distancelist = [-1]*(N+1)
queue = deque()
queue.append(1)
distancelist[1] = 0
while queue:
    node = queue.popleft()
    distance = distancelist[node]
    for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor)

for i in range(1, N+1):
    print(i, distancelist[i]) from collections import deque from sys import stdin input = stdin.readline input stdin.readline stdin readline N = int(input()) N int(input()) int input() input None for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) None neighborlist = [None for _ in range(N+1)] neighborlist [None for _ in range(N+1)] for _ in range(1, N+1):
    node, _, *L = list(map(int, input().split()))
    neighborlist[node] = L _ range(1, N+1) range 1 N+1 N 1 node, _, *L = list(map(int, input().split())) node list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split _ *L L neighborlist[node] = L neighborlist[node] neighborlist node L distancelist = [-1]*(N+1) distancelist [-1]*(N+1) [-1] -1 N+1 N 1 queue = deque() queue deque() deque queue.append(1) queue.append queue append 1 distancelist[1] = 0 distancelist[1] distancelist 1 0 while queue:
    node = queue.popleft()
    distance = distancelist[node]
    for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor) queue node = queue.popleft() node queue.popleft() queue.popleft queue popleft distance = distancelist[node] distance distancelist[node] distancelist node for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor) neighbor neighborlist[node] neighborlist node if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor) distancelist[neighbor] == -1 distancelist[neighbor] distancelist neighbor -1 distancelist[neighbor] = distance + 1 distancelist[neighbor] distancelist neighbor distance + 1 distance 1 queue.append(neighbor) queue.append queue append neighbor for i in range(1, N+1):
    print(i, distancelist[i]) i range(1, N+1) range 1 N+1 N 1 print(i, distancelist[i]) print i distancelist[i] distancelist i distance = distancelist[node] distancelist[node] distance node, _, *L = list(map(int, input().split())) list(map(int, input().split())) node node = queue.popleft() queue.popleft() node neighborlist = [None for _ in range(N+1)] [None for _ in range(N+1)] neighborlist distancelist = [-1]*(N+1) [-1]*(N+1) distancelist input = stdin.readline stdin.readline input N = int(input()) int(input()) N _, *L = list(map(int, input().split())) list(map(int, input().split())) _ queue = deque() deque() queue
from collections import deque
from sys import stdin
input = stdin.readline

N = int(input())
neighborlist = [None for _ in range(N+1)]
for _ in range(1, N+1):
    node, _, *L = list(map(int, input().split()))
    neighborlist[node] = L

distancelist = [-1]*(N+1)
queue = deque()
queue.append(1)
distancelist[1] = 0
while queue:
    node = queue.popleft()
    distance = distancelist[node]
    for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor)

for i in range(1, N+1):
    print(i, distancelist[i])
