import collections
n= int(input())
g=[]
for _ in range(n):
    v,k,*varray= map(int,input().split())
    g.append(varray)
        

d= [-1]* (n+10)
d[0]=0
q= collections.deque()
q.append(0)
while len(q)>  0:
    cur = q.popleft()
    for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
            
for i in range(n):
    print(i+1,d[i])
     import collections collections n= int(input()) n int(input()) int input() input g=[] g [] for _ in range(n):
    v,k,*varray= map(int,input().split())
    g.append(varray)
         _ range(n) range n v,k,*varray= map(int,input().split()) v map(int,input().split()) map int input().split() ().split () input split k *varray varray g.append(varray) g.append g append varray d= [-1]* (n+10) d [-1]* (n+10) [-1] -1 n+10 n 10 d[0]=0 d[0] d 0 0 q= collections.deque() q collections.deque() collections.deque collections deque q.append(0) q.append q append 0 while len(q)>  0:
    cur = q.popleft()
    for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
             len(q)>  0 len(q) len q 0 cur = q.popleft() cur q.popleft() q.popleft q popleft for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
             next g[cur] g cur if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
             d[next-1]== -1 d[next-1] d next-1 next 1 -1 d[next-1]= d[cur]+1 d[next-1] d next-1 next 1 d[cur]+1 d[cur] d cur 1 q.append(next-1) q.append q append next-1 next 1 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 d= [-1]* (n+10) [-1]* (n+10) d q= collections.deque() collections.deque() q n= int(input()) int(input()) n cur = q.popleft() q.popleft() cur g=[] [] g v,k,*varray= map(int,input().split()) map(int,input().split()) v k,*varray= map(int,input().split()) map(int,input().split()) k
import collections
n= int(input())
g=[]
for _ in range(n):
    v,k,*varray= map(int,input().split())
    g.append(varray)
        

d= [-1]* (n+10)
d[0]=0
q= collections.deque()
q.append(0)
while len(q)>  0:
    cur = q.popleft()
    for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
            
for i in range(n):
    print(i+1,d[i])
    
