from collections import deque
INF = 1145141919810364364334
s = [None]
r = [None]

n = int(input())
for _ in range(n):
    temp = list(map(int,input().split()))
    s.append(temp[2:])
    
d = [INF] * (n+1)

d[1] = 0
q = deque()
q.append(1)

while q:
    x = q.popleft()
    for i in s[x]:
        if d[i] == INF:
            q.append(i)
            
        d[i] = min(d[x] + 1 , d[i])
        
for i in range(1,n+1):
    if d[i] == INF:
        d[i] = -1
    print(i,d[i]) from collections import deque INF = 1145141919810364364334 INF 1145141919810364364334 s = [None] s [None] None r = [None] r [None] None n = int(input()) n int(input()) int input() input for _ in range(n):
    temp = list(map(int,input().split()))
    s.append(temp[2:])
     _ range(n) range n temp = list(map(int,input().split())) temp list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split s.append(temp[2:]) s.append s append temp[2:] temp 2: 2 d = [INF] * (n+1) d [INF] * (n+1) [INF] INF n+1 n 1 d[1] = 0 d[1] d 1 0 q = deque() q deque() deque q.append(1) q.append q append 1 while q:
    x = q.popleft()
    for i in s[x]:
        if d[i] == INF:
            q.append(i)
            
        d[i] = min(d[x] + 1 , d[i])
         q x = q.popleft() x q.popleft() q.popleft q popleft for i in s[x]:
        if d[i] == INF:
            q.append(i)
            
        d[i] = min(d[x] + 1 , d[i])
         i s[x] s x if d[i] == INF:
            q.append(i)
            
         d[i] == INF d[i] d i INF q.append(i) q.append q append i d[i] = min(d[x] + 1 , d[i]) d[i] d i min(d[x] + 1 , d[i]) min d[x] + 1 d[x] d x 1 d[i] d i for i in range(1,n+1):
    if d[i] == INF:
        d[i] = -1
    print(i,d[i]) i range(1,n+1) range 1 n+1 n 1 if d[i] == INF:
        d[i] = -1
     d[i] == INF d[i] d i INF d[i] = -1 d[i] d i -1 print(i,d[i]) print i d[i] d i x = q.popleft() q.popleft() x n = int(input()) int(input()) n q = deque() deque() q s = [None] [None] s r = [None] [None] r INF = 1145141919810364364334 1145141919810364364334 INF temp = list(map(int,input().split())) list(map(int,input().split())) temp d = [INF] * (n+1) [INF] * (n+1) d
from collections import deque
INF = 1145141919810364364334
s = [None]
r = [None]

n = int(input())
for _ in range(n):
    temp = list(map(int,input().split()))
    s.append(temp[2:])
    
d = [INF] * (n+1)

d[1] = 0
q = deque()
q.append(1)

while q:
    x = q.popleft()
    for i in s[x]:
        if d[i] == INF:
            q.append(i)
            
        d[i] = min(d[x] + 1 , d[i])
        
for i in range(1,n+1):
    if d[i] == INF:
        d[i] = -1
    print(i,d[i])
