from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
for i in range(N):
  X = list(map(int, input().split()))
  for j in range(2, len(X)):
    graph[i].append(X[j]-1)
    
dist = [-1] * N
dist[0] = 0

q = deque([])
q.append(0)

while q:
  x = q.popleft()
  for next_x in graph[x]:
    if(dist[next_x] > -1):
      continue
    dist[next_x] = dist[x] + 1
    q.append(next_x)
    
for i in range(N):
  print(i+1, dist[i]) from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] for i in range(N):
  X = list(map(int, input().split()))
  for j in range(2, len(X)):
    graph[i].append(X[j]-1)
     i range(N) range N X = list(map(int, input().split())) X list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split for j in range(2, len(X)):
    graph[i].append(X[j]-1)
     j range(2, len(X)) range 2 len(X) len X graph[i].append(X[j]-1) [i].append [i] graph i append X[j]-1 X[j] X j 1 dist = [-1] * N dist [-1] * N [-1] -1 N dist[0] = 0 dist[0] dist 0 0 q = deque([]) q deque([]) deque [] q.append(0) q.append q append 0 while q:
  x = q.popleft()
  for next_x in graph[x]:
    if(dist[next_x] > -1):
      continue
    dist[next_x] = dist[x] + 1
    q.append(next_x)
     q x = q.popleft() x q.popleft() q.popleft q popleft for next_x in graph[x]:
    if(dist[next_x] > -1):
      continue
    dist[next_x] = dist[x] + 1
    q.append(next_x)
     next_x graph[x] graph x if(dist[next_x] > -1):
      continue
     dist[next_x] > -1 dist[next_x] dist next_x -1 continue dist[next_x] = dist[x] + 1 dist[next_x] dist next_x dist[x] + 1 dist[x] dist x 1 q.append(next_x) q.append q append next_x for i in range(N):
  print(i+1, dist[i]) i range(N) range N print(i+1, dist[i]) print i+1 i 1 dist[i] dist i N = int(input()) int(input()) N graph = [[] for _ in range(N)] [[] for _ in range(N)] graph q = deque([]) deque([]) q dist = [-1] * N [-1] * N dist X = list(map(int, input().split())) list(map(int, input().split())) X x = q.popleft() q.popleft() x
from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
for i in range(N):
  X = list(map(int, input().split()))
  for j in range(2, len(X)):
    graph[i].append(X[j]-1)
    
dist = [-1] * N
dist[0] = 0

q = deque([])
q.append(0)

while q:
  x = q.popleft()
  for next_x in graph[x]:
    if(dist[next_x] > -1):
      continue
    dist[next_x] = dist[x] + 1
    q.append(next_x)
    
for i in range(N):
  print(i+1, dist[i])
