n=int(input())
G=[[0,[],0,-1]]
for i in range(n):
    v=[int(j) for j in input().split(" ")]
    u=v.pop(0)
    k=v.pop(0)
    G.append([k,v,0,-1])





distance=0
dis_list=[1]
G[1][3]=0
G[1][2]=1
while len(dis_list)!=0:
    distance+=1
    dis_list_cash=[]
    for i in dis_list:
        k=G[i][0]
        v=G[i][1]
        for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
    dis_list=dis_list_cash



for i in range(1,n+1):
    print(str(i)+" "+str(G[i][3])) n=int(input()) n int(input()) int input() input G=[[0,[],0,-1]] G [[0,[],0,-1]] [0,[],0,-1] 0 [] 0 -1 for i in range(n):
    v=[int(j) for j in input().split(" ")]
    u=v.pop(0)
    k=v.pop(0)
    G.append([k,v,0,-1]) i range(n) range n int(j) for j in input().split(" ") for j in input().split(" ") j input().split(" ") ().split () input split " " for j in input().split(" ") int(j) int j v=[int(j) for j in input().split(" ")] v [int(j) for j in input().split(" ")] u=v.pop(0) u v.pop(0) v.pop v pop 0 k=v.pop(0) k v.pop(0) v.pop v pop 0 G.append([k,v,0,-1]) G.append G append [k,v,0,-1] k v 0 -1 distance=0 distance 0 dis_list=[1] dis_list [1] 1 G[1][3]=0 G[1][3] [1] G 1 3 0 G[1][2]=1 G[1][2] [1] G 1 2 1 while len(dis_list)!=0:
    distance+=1
    dis_list_cash=[]
    for i in dis_list:
        k=G[i][0]
        v=G[i][1]
        for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
    dis_list=dis_list_cash len(dis_list)!=0 len(dis_list) len dis_list 0 distance+=1 distance 1 dis_list_cash=[] dis_list_cash [] for i in dis_list:
        k=G[i][0]
        v=G[i][1]
        for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
     i dis_list k=G[i][0] k G[i][0] [i] G i 0 v=G[i][1] v G[i][1] [i] G i 1 for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
     j range(k) range k if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
     G[v[j]][2]==0 G[v[j]][2] [v[j]] G v[j] v j 2 0 G[v[j]][2]=1 G[v[j]][2] [v[j]] G v[j] v j 2 1 G[v[j]][3]=distance G[v[j]][3] [v[j]] G v[j] v j 3 distance dis_list_cash.append(v[j]) dis_list_cash.append dis_list_cash append v[j] v j dis_list=dis_list_cash dis_list dis_list_cash for i in range(1,n+1):
    print(str(i)+" "+str(G[i][3])) i range(1,n+1) range 1 n+1 n 1 print(str(i)+" "+str(G[i][3])) print str(i)+" "+str(G[i][3]) str(i)+" " str(i) str i " " str(G[i][3]) str G[i][3] [i] G i 3 u=v.pop(0) v.pop(0) u dis_list=dis_list_cash dis_list_cash dis_list k=v.pop(0) v.pop(0) k distance=0 0 distance dis_list_cash=[] [] dis_list_cash dis_list=[1] [1] dis_list distance+=1 1 distance G=[[0,[],0,-1]] [[0,[],0,-1]] G v=G[i][1] G[i][1] v v=[int(j) for j in input().split(" ")] [int(j) for j in input().split(" ")] v k=G[i][0] G[i][0] k n=int(input()) int(input()) n
n=int(input())
G=[[0,[],0,-1]]
for i in range(n):
    v=[int(j) for j in input().split(" ")]
    u=v.pop(0)
    k=v.pop(0)
    G.append([k,v,0,-1])





distance=0
dis_list=[1]
G[1][3]=0
G[1][2]=1
while len(dis_list)!=0:
    distance+=1
    dis_list_cash=[]
    for i in dis_list:
        k=G[i][0]
        v=G[i][1]
        for j in range(k):
            if G[v[j]][2]==0:
                G[v[j]][2]=1
                G[v[j]][3]=distance
                dis_list_cash.append(v[j])
    
    dis_list=dis_list_cash



for i in range(1,n+1):
    print(str(i)+" "+str(G[i][3]))