from collections import deque

def bfs(x):
  for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
  if len(queue) == 0:
    return
  bfs(queue.popleft())
  
N = int(input())
graph = []
for _ in range(N):
  graph.append(list(map(lambda x: int(x)-1, input().split()))[2:])
dist = [-1] * N
queue = deque()

dist[0] = 0
bfs(0)
for i in range(N):
  print("{} {}".format(i+1, dist[i])) from collections import deque def bfs(x):
  for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
  if len(queue) == 0:
    return
  bfs(queue.popleft())
   bfs for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
   v graph[x] graph x if dist[v] != -1:
      continue
     dist[v] != -1 dist[v] dist v -1 continue dist[v] = dist[x] + 1 dist[v] dist v dist[x] + 1 dist[x] dist x 1 queue.append(v) queue.append queue append v if len(queue) == 0:
    return
   len(queue) == 0 len(queue) len queue 0 return bfs(queue.popleft()) bfs queue.popleft() queue.popleft queue popleft x x N = int(input()) N int(input()) int input() input graph = [] graph [] for _ in range(N):
  graph.append(list(map(lambda x: int(x)-1, input().split()))[2:]) _ range(N) range N graph.append(list(map(lambda x: int(x)-1, input().split()))[2:]) graph.append graph append list(map(lambda x: int(x)-1, input().split()))[2:] (map(lambda x: int(x)-1, input().split())) list map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split 2: 2 dist = [-1] * N dist [-1] * N [-1] -1 N queue = deque() queue deque() deque dist[0] = 0 dist[0] dist 0 0 bfs(0) bfs 0 for i in range(N):
  print("{} {}".format(i+1, dist[i])) i range(N) range N print("{} {}".format(i+1, dist[i])) print "{} {}".format(i+1, dist[i]) "{} {}".format "{} {}" format i+1 i 1 dist[i] dist i graph = [] [] graph queue = deque() deque() queue dist = [-1] * N [-1] * N dist def bfs(x):
  for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
  if len(queue) == 0:
    return
  bfs(queue.popleft())
   def bfs(x):
  for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
  if len(queue) == 0:
    return
  bfs(queue.popleft())
   bfs N = int(input()) int(input()) N
from collections import deque

def bfs(x):
  for v in graph[x]:
    if dist[v] != -1:
      continue
    dist[v] = dist[x] + 1
    queue.append(v)
  if len(queue) == 0:
    return
  bfs(queue.popleft())
  
N = int(input())
graph = []
for _ in range(N):
  graph.append(list(map(lambda x: int(x)-1, input().split()))[2:])
dist = [-1] * N
queue = deque()

dist[0] = 0
bfs(0)
for i in range(N):
  print("{} {}".format(i+1, dist[i]))
