import queue

n = int(input())
graph = {}
for _ in range(n):
  i = list(map(int,input().split()))
  graph[i[0]] = i[2:]

dist = [-1 for i in range(n)]
todo = queue.Queue()
seen = set()

dist[0] = 0
todo.put(1)

while (not todo.empty()):
  now = todo.get()
  for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx)

for i in range(n):
  print(i+1, dist[i])

 import queue queue n = int(input()) n int(input()) int input() input graph = {} graph {} for _ in range(n):
  i = list(map(int,input().split()))
  graph[i[0]] = i[2:] _ range(n) range n i = list(map(int,input().split())) i list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split graph[i[0]] = i[2:] graph[i[0]] graph i[0] i 0 i[2:] i 2: 2 -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 dist = [-1 for i in range(n)] dist [-1 for i in range(n)] todo = queue.Queue() todo queue.Queue() queue.Queue queue Queue seen = set() seen set() set dist[0] = 0 dist[0] dist 0 0 todo.put(1) todo.put todo put 1 while (not todo.empty()):
  now = todo.get()
  for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx) not todo.empty() todo.empty() todo.empty todo empty now = todo.get() now todo.get() todo.get todo get for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx) nx graph[now] graph now if (dist[nx-1] != -1):
      continue
     dist[nx-1] != -1 dist[nx-1] dist nx-1 nx 1 -1 continue dist[nx-1] = dist[now-1] + 1 dist[nx-1] dist nx-1 nx 1 dist[now-1] + 1 dist[now-1] dist now-1 now 1 1 todo.put(nx) todo.put todo put nx 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 todo = queue.Queue() queue.Queue() todo n = int(input()) int(input()) n graph = {} {} graph dist = [-1 for i in range(n)] [-1 for i in range(n)] dist i = list(map(int,input().split())) list(map(int,input().split())) i now = todo.get() todo.get() now seen = set() set() seen
import queue

n = int(input())
graph = {}
for _ in range(n):
  i = list(map(int,input().split()))
  graph[i[0]] = i[2:]

dist = [-1 for i in range(n)]
todo = queue.Queue()
seen = set()

dist[0] = 0
todo.put(1)

while (not todo.empty()):
  now = todo.get()
  for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx)

for i in range(n):
  print(i+1, dist[i])


