from collections import deque
n = int(input())
adj = [ [] for _ in range(n+1) ]
for _ in range(n):
  lst = list(map(int,input().split()))
  if lst[1] > 0:
    adj[lst[0]] = lst[2:]

q = deque([1])
dist = [-1] * (n+1)
dist[1] = 0

q = deque([1])
while q:
  node = q.popleft()

  for nei in adj[node]:
    if dist[nei] != -1: continue
    dist[nei] = dist[node] + 1
    q.append(nei)

for i,s in enumerate(dist):
  if i==0: continue
  print(i, s)

 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) [] adj = [ [] for _ in range(n+1) ] adj [ [] for _ in range(n+1) ] for _ in range(n):
  lst = list(map(int,input().split()))
  if lst[1] > 0:
    adj[lst[0]] = lst[2:] _ range(n) range n lst = list(map(int,input().split())) lst list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if lst[1] > 0:
    adj[lst[0]] = lst[2:] lst[1] > 0 lst[1] lst 1 0 adj[lst[0]] = lst[2:] adj[lst[0]] adj lst[0] lst 0 lst[2:] lst 2: 2 q = deque([1]) q deque([1]) deque [1] 1 dist = [-1] * (n+1) dist [-1] * (n+1) [-1] -1 n+1 n 1 dist[1] = 0 dist[1] dist 1 0 q = deque([1]) q deque([1]) deque [1] 1 while q:
  node = q.popleft()

  for nei in adj[node]:
    if dist[nei] != -1: continue
    dist[nei] = dist[node] + 1
    q.append(nei) q node = q.popleft() node q.popleft() q.popleft q popleft for nei in adj[node]:
    if dist[nei] != -1: continue
    dist[nei] = dist[node] + 1
    q.append(nei) nei adj[node] adj node if dist[nei] != -1: continue
     dist[nei] != -1 dist[nei] dist nei -1 dist[nei] = dist[node] + 1 dist[nei] dist nei dist[node] + 1 dist[node] dist node 1 q.append(nei) q.append q append nei for i,s in enumerate(dist):
  if i==0: continue
  print(i, s) i s enumerate(dist) enumerate dist if i==0: continue
   i==0 i 0 print(i, s) print i s lst = list(map(int,input().split())) list(map(int,input().split())) lst adj = [ [] for _ in range(n+1) ] [ [] for _ in range(n+1) ] adj q = deque([1]) deque([1]) q dist = [-1] * (n+1) [-1] * (n+1) dist q = deque([1]) deque([1]) q node = q.popleft() q.popleft() node n = int(input()) int(input()) n
from collections import deque
n = int(input())
adj = [ [] for _ in range(n+1) ]
for _ in range(n):
  lst = list(map(int,input().split()))
  if lst[1] > 0:
    adj[lst[0]] = lst[2:]

q = deque([1])
dist = [-1] * (n+1)
dist[1] = 0

q = deque([1])
while q:
  node = q.popleft()

  for nei in adj[node]:
    if dist[nei] != -1: continue
    dist[nei] = dist[node] + 1
    q.append(nei)

for i,s in enumerate(dist):
  if i==0: continue
  print(i, s)


