from collections import deque
n = int(input())
d = [-1]*n
d[0] = 0
M = []
for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]]
Q = deque([0])

while Q != deque([]):
  u = Q.popleft()
  for i in range(len(M[u])):
    v = M[u][i]-1
    if d[v] == -1:
      d[v] = d[u] + 1
      Q.append(v)
for i in range(n):
  print(i+1,d[i])
                 
                 
   from collections import deque n = int(input()) n int(input()) int input() input d = [-1]*n d [-1]*n [-1] -1 n d[0] = 0 d[0] d 0 0 M = [] M [] for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]] i range(n) range n adj = list(map(int,input().split())) adj list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]] adj[1] == 0 adj[1] adj 1 0 M += [[]] M [[]] [] M += [adj[2:]] M [adj[2:]] adj[2:] adj 2: 2 Q = deque([0]) Q deque([0]) deque [0] 0 while Q != deque([]):
  u = Q.popleft()
  for i in range(len(M[u])):
    v = M[u][i]-1
    if d[v] == -1:
      d[v] = d[u] + 1
      Q.append(v) Q != deque([]) Q deque([]) deque [] u = Q.popleft() u Q.popleft() Q.popleft Q popleft for i in range(len(M[u])):
    v = M[u][i]-1
    if d[v] == -1:
      d[v] = d[u] + 1
      Q.append(v) i range(len(M[u])) range len(M[u]) len M[u] M u v = M[u][i]-1 v M[u][i]-1 M[u][i] [u] M u i 1 if d[v] == -1:
      d[v] = d[u] + 1
      Q.append(v) d[v] == -1 d[v] d v -1 d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 Q.append(v) Q.append Q append v for i in range(n):
  print(i+1,d[i])
                 
                 
   i range(n) range n print(i+1,d[i]) print i+1 i 1 d[i] d i Q = deque([0]) deque([0]) Q u = Q.popleft() Q.popleft() u n = int(input()) int(input()) n M += [adj[2:]] [adj[2:]] M M = [] [] M d = [-1]*n [-1]*n d M += [[]] [[]] M adj = list(map(int,input().split())) list(map(int,input().split())) adj v = M[u][i]-1 M[u][i]-1 v
from collections import deque
n = int(input())
d = [-1]*n
d[0] = 0
M = []
for i in range(n):
    adj = list(map(int,input().split()))
    if adj[1] == 0:
        M += [[]]
    else:
        M += [adj[2:]]
Q = deque([0])

while Q != deque([]):
  u = Q.popleft()
  for i in range(len(M[u])):
    v = M[u][i]-1
    if d[v] == -1:
      d[v] = d[u] + 1
      Q.append(v)
for i in range(n):
  print(i+1,d[i])
                 
                 
  
