from collections import deque

n = int(input())
adj = [[] for _ in range(n)]

for i in range(n):
  u,k,*v = map(int, input().split())
  v = [w-1 for w in v]
  adj[u-1].extend(v)

queue = deque([0])
ans = [-1] * n
ans[0] = 0

while queue:
  now = queue.popleft()
  
  for u in adj[now]:
    if ans[u] < 0:# 未訪問
      ans[u] = ans[now] + 1
      queue.append(u)

for i in range(n):
  print(i+1,ans[i]) from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] adj = [[] for _ in range(n)] adj [[] for _ in range(n)] for i in range(n):
  u,k,*v = map(int, input().split())
  v = [w-1 for w in v]
  adj[u-1].extend(v) i range(n) range n u,k,*v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v w-1 for w in v for w in v w v for w in v w-1 w 1 v = [w-1 for w in v] v [w-1 for w in v] adj[u-1].extend(v) [u-1].extend [u-1] adj u-1 u 1 extend v queue = deque([0]) queue deque([0]) deque [0] 0 ans = [-1] * n ans [-1] * n [-1] -1 n ans[0] = 0 ans[0] ans 0 0 while queue:
  now = queue.popleft()
  
  for u in adj[now]:
    if ans[u] < 0:# 未訪問
      ans[u] = ans[now] + 1
      queue.append(u) queue now = queue.popleft() now queue.popleft() queue.popleft queue popleft for u in adj[now]:
    if ans[u] < 0:# 未訪問
      ans[u] = ans[now] + 1
      queue.append(u) u adj[now] adj now if ans[u] < 0:# 未訪問
      ans[u] = ans[now] + 1
      queue.append(u) ans[u] < 0 ans[u] ans u 0 ans[u] = ans[now] + 1 ans[u] ans u ans[now] + 1 ans[now] ans now 1 queue.append(u) queue.append queue append u for i in range(n):
  print(i+1,ans[i]) i range(n) range n print(i+1,ans[i]) print i+1 i 1 ans[i] ans i k,*v = map(int, input().split()) map(int, input().split()) k adj = [[] for _ in range(n)] [[] for _ in range(n)] adj ans = [-1] * n [-1] * n ans u,k,*v = map(int, input().split()) map(int, input().split()) u now = queue.popleft() queue.popleft() now v = [w-1 for w in v] [w-1 for w in v] v n = int(input()) int(input()) n queue = deque([0]) deque([0]) queue
from collections import deque

n = int(input())
adj = [[] for _ in range(n)]

for i in range(n):
  u,k,*v = map(int, input().split())
  v = [w-1 for w in v]
  adj[u-1].extend(v)

queue = deque([0])
ans = [-1] * n
ans[0] = 0

while queue:
  now = queue.popleft()
  
  for u in adj[now]:
    if ans[u] < 0:# 未訪問
      ans[u] = ans[now] + 1
      queue.append(u)

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