from collections import deque
from enum import Enum

class Color(Enum):
  WHITE = 0
  GRAY = 1
  BLACK = 2


N = int(input())
adj = [list(map(int, input().split()))[2:] for i in range(N)]


colors = [Color.WHITE for i in range(N)]
INF = N
d = [-1 for i in range(N)]
d[0] = 0

q = deque()
q.append(1)
colors[0] == Color.GRAY
while len(q) > 0:
  u = q.popleft()-1
  for v in adj[u]:
    v -= 1
    if colors[v] != Color.WHITE:
      continue
    d[v] = d[u] + 1
    colors[v] = Color.GRAY
    q.append(v+1)
  colors[u] = Color.BLACK

for i in range(N):
  print(i+1, d[i]) from collections import deque from enum import Enum class Color(Enum):
  WHITE = 0
  GRAY = 1
  BLACK = 2 Color WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 Enum N = int(input()) N int(input()) int input() input list(map(int, input().split()))[2:] for i in range(N) for i in range(N) i range(N) range N for i in range(N) list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 adj = [list(map(int, input().split()))[2:] for i in range(N)] adj [list(map(int, input().split()))[2:] for i in range(N)] Color.WHITE for i in range(N) for i in range(N) i range(N) range N for i in range(N) Color.WHITE Color WHITE colors = [Color.WHITE for i in range(N)] colors [Color.WHITE for i in range(N)] INF = N INF N -1 for i in range(N) for i in range(N) i range(N) range N for i in range(N) -1 d = [-1 for i in range(N)] d [-1 for i in range(N)] d[0] = 0 d[0] d 0 0 q = deque() q deque() deque q.append(1) q.append q append 1 colors[0] == Color.GRAY colors[0] colors 0 Color.GRAY Color GRAY while len(q) > 0:
  u = q.popleft()-1
  for v in adj[u]:
    v -= 1
    if colors[v] != Color.WHITE:
      continue
    d[v] = d[u] + 1
    colors[v] = Color.GRAY
    q.append(v+1)
  colors[u] = Color.BLACK len(q) > 0 len(q) len q 0 u = q.popleft()-1 u q.popleft()-1 q.popleft() q.popleft q popleft 1 for v in adj[u]:
    v -= 1
    if colors[v] != Color.WHITE:
      continue
    d[v] = d[u] + 1
    colors[v] = Color.GRAY
    q.append(v+1)
   v adj[u] adj u v -= 1 v 1 if colors[v] != Color.WHITE:
      continue
     colors[v] != Color.WHITE colors[v] colors v Color.WHITE Color WHITE continue d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 colors[v] = Color.GRAY colors[v] colors v Color.GRAY Color GRAY q.append(v+1) q.append q append v+1 v 1 colors[u] = Color.BLACK colors[u] colors u Color.BLACK Color BLACK for i in range(N):
  print(i+1, d[i]) i range(N) range N print(i+1, d[i]) print i+1 i 1 d[i] d i v -= 1 1 v class Color(Enum):
  WHITE = 0
  GRAY = 1
  BLACK = 2 class Color(Enum):
  WHITE = 0
  GRAY = 1
  BLACK = 2 Color INF = N N INF d = [-1 for i in range(N)] [-1 for i in range(N)] d colors = [Color.WHITE for i in range(N)] [Color.WHITE for i in range(N)] colors u = q.popleft()-1 q.popleft()-1 u N = int(input()) int(input()) N q = deque() deque() q adj = [list(map(int, input().split()))[2:] for i in range(N)] [list(map(int, input().split()))[2:] for i in range(N)] adj
from collections import deque
from enum import Enum

class Color(Enum):
  WHITE = 0
  GRAY = 1
  BLACK = 2


N = int(input())
adj = [list(map(int, input().split()))[2:] for i in range(N)]


colors = [Color.WHITE for i in range(N)]
INF = N
d = [-1 for i in range(N)]
d[0] = 0

q = deque()
q.append(1)
colors[0] == Color.GRAY
while len(q) > 0:
  u = q.popleft()-1
  for v in adj[u]:
    v -= 1
    if colors[v] != Color.WHITE:
      continue
    d[v] = d[u] + 1
    colors[v] = Color.GRAY
    q.append(v+1)
  colors[u] = Color.BLACK

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