from collections import deque

N = int(input())
inf = 10 ** 6
graph = [[] for i in range(N)]
distance = [inf for i in range(N)]

for i in range(N):
  a = list(map(int,input().split()))
  a = list(map(lambda x: x - 1,a))
  k,n = a[0],a[1]
  if n >= 0:
    graph[k] = a[2:]
    

def bfs(path,root,dist):
  dist[root] = 0
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi])

bfs(graph,0,distance)
distance = [-1 if i == inf else i for i in distance]

for i,j in enumerate(distance):
  print(i + 1,j)
   from collections import deque N = int(input()) N int(input()) int input() input inf = 10 ** 6 inf 10 ** 6 10 6 [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] graph = [[] for i in range(N)] graph [[] for i in range(N)] inf for i in range(N) for i in range(N) i range(N) range N for i in range(N) inf distance = [inf for i in range(N)] distance [inf for i in range(N)] for i in range(N):
  a = list(map(int,input().split()))
  a = list(map(lambda x: x - 1,a))
  k,n = a[0],a[1]
  if n >= 0:
    graph[k] = a[2:]
     i range(N) range N a = list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split a = list(map(lambda x: x - 1,a)) a list(map(lambda x: x - 1,a)) list map(lambda x: x - 1,a) map lambda x: x - 1 x - 1 x 1 x a k,n = a[0],a[1] k a[0] a 0 n a[1] a 1 if n >= 0:
    graph[k] = a[2:]
     n >= 0 n 0 graph[k] = a[2:] graph[k] graph k a[2:] a 2: 2 def bfs(path,root,dist):
  dist[root] = 0
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi]) bfs dist[root] = 0 dist[root] dist root 0 queue = deque([root]) queue deque([root]) deque [root] root while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi]) queue cur = queue.popleft() cur queue.popleft() queue.popleft queue popleft for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi]) chi path[cur] path cur if dist[chi] == inf:
        queue.append(chi)
       dist[chi] == inf dist[chi] dist chi inf queue.append(chi) queue.append queue append chi dist[chi] = min(dist[cur] + 1,dist[chi]) dist[chi] dist chi min(dist[cur] + 1,dist[chi]) min dist[cur] + 1 dist[cur] dist cur 1 dist[chi] dist chi path path root root dist dist bfs(graph,0,distance) bfs graph 0 distance -1 if i == inf else i for i in distance for i in distance i distance for i in distance -1 if i == inf else i i == inf i inf -1 i distance = [-1 if i == inf else i for i in distance] distance [-1 if i == inf else i for i in distance] for i,j in enumerate(distance):
  print(i + 1,j)
   i j enumerate(distance) enumerate distance print(i + 1,j) print i + 1 i 1 j N = int(input()) int(input()) N a = list(map(int,input().split())) list(map(int,input().split())) a inf = 10 ** 6 10 ** 6 inf distance = [inf for i in range(N)] [inf for i in range(N)] distance distance = [-1 if i == inf else i for i in distance] [-1 if i == inf else i for i in distance] distance def bfs(path,root,dist):
  dist[root] = 0
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi]) def bfs(path,root,dist):
  dist[root] = 0
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi]) bfs a = list(map(lambda x: x - 1,a)) list(map(lambda x: x - 1,a)) a graph = [[] for i in range(N)] [[] for i in range(N)] graph n = a[0],a[1] a[1] n k,n = a[0],a[1] a[0] k
from collections import deque

N = int(input())
inf = 10 ** 6
graph = [[] for i in range(N)]
distance = [inf for i in range(N)]

for i in range(N):
  a = list(map(int,input().split()))
  a = list(map(lambda x: x - 1,a))
  k,n = a[0],a[1]
  if n >= 0:
    graph[k] = a[2:]
    

def bfs(path,root,dist):
  dist[root] = 0
  queue = deque([root])
  while queue:
    cur = queue.popleft()
    for chi in path[cur]:
      if dist[chi] == inf:
        queue.append(chi)
      dist[chi] = min(dist[cur] + 1,dist[chi])

bfs(graph,0,distance)
distance = [-1 if i == inf else i for i in distance]

for i,j in enumerate(distance):
  print(i + 1,j)
  
