from collections import deque
q = deque([])
n = int(input())
G = []
for i in range(n):
  u, k, *vvv = list(map(int, input().split()))
  G.append([v-1 for v in vvv])
visited = [False for _ in range(n)]
q.append(0)
dist = [-1 for _ in range(n)]
dist[0] = 0
while q:
  u = q.popleft()
  visited[u] = True
  for v in G[u]:
    if visited[v] == True:
      continue
    else:
      q.append(v)
      visited[v] = True
      dist[v] = dist[u] + 1
    
for i in range(n):
  print(i+1, dist[i])
   from collections import deque q = deque([]) q deque([]) deque [] n = int(input()) n int(input()) int input() input G = [] G [] for i in range(n):
  u, k, *vvv = list(map(int, input().split()))
  G.append([v-1 for v in vvv]) i range(n) range n u, k, *vvv = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split k *vvv vvv v-1 for v in vvv for v in vvv v vvv for v in vvv v-1 v 1 G.append([v-1 for v in vvv]) G.append G append [v-1 for v in vvv] False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False visited = [False for _ in range(n)] visited [False for _ in range(n)] q.append(0) q.append q append 0 -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 dist = [-1 for _ in range(n)] dist [-1 for _ in range(n)] dist[0] = 0 dist[0] dist 0 0 while q:
  u = q.popleft()
  visited[u] = True
  for v in G[u]:
    if visited[v] == True:
      continue
    else:
      q.append(v)
      visited[v] = True
      dist[v] = dist[u] + 1
     q u = q.popleft() u q.popleft() q.popleft q popleft visited[u] = True visited[u] visited u True for v in G[u]:
    if visited[v] == True:
      continue
    else:
      q.append(v)
      visited[v] = True
      dist[v] = dist[u] + 1
     v G[u] G u if visited[v] == True:
      continue
    else:
      q.append(v)
      visited[v] = True
      dist[v] = dist[u] + 1
     visited[v] == True visited[v] visited v True continue q.append(v) q.append q append v visited[v] = True visited[v] visited v True dist[v] = dist[u] + 1 dist[v] dist v dist[u] + 1 dist[u] dist u 1 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 dist = [-1 for _ in range(n)] [-1 for _ in range(n)] dist q = deque([]) deque([]) q n = int(input()) int(input()) n u, k, *vvv = list(map(int, input().split())) list(map(int, input().split())) u u = q.popleft() q.popleft() u G = [] [] G visited = [False for _ in range(n)] [False for _ in range(n)] visited k, *vvv = list(map(int, input().split())) list(map(int, input().split())) k
from collections import deque
q = deque([])
n = int(input())
G = []
for i in range(n):
  u, k, *vvv = list(map(int, input().split()))
  G.append([v-1 for v in vvv])
visited = [False for _ in range(n)]
q.append(0)
dist = [-1 for _ in range(n)]
dist[0] = 0
while q:
  u = q.popleft()
  visited[u] = True
  for v in G[u]:
    if visited[v] == True:
      continue
    else:
      q.append(v)
      visited[v] = True
      dist[v] = dist[u] + 1
    
for i in range(n):
  print(i+1, dist[i])
  
