n = int(input())
v = []
for i in range(n):
    v_i = list(map(int,input().split()))[2:]
    v.append(v_i)
    
d = [-1]*n
d[0]=0
queue = [1]
dis = 1
while len(queue)!=0:
    queue2 = []
    for i in queue:
        for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
    dis +=1
    queue = queue2

for i in range(n):
    print(i+1,d[i]) n = int(input()) n int(input()) int input() input v = [] v [] for i in range(n):
    v_i = list(map(int,input().split()))[2:]
    v.append(v_i)
     i range(n) range n v_i = list(map(int,input().split()))[2:] v_i list(map(int,input().split()))[2:] (map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 2: 2 v.append(v_i) v.append v append v_i d = [-1]*n d [-1]*n [-1] -1 n d[0]=0 d[0] d 0 0 queue = [1] queue [1] 1 dis = 1 dis 1 while len(queue)!=0:
    queue2 = []
    for i in queue:
        for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
    dis +=1
    queue = queue2 len(queue)!=0 len(queue) len queue 0 queue2 = [] queue2 [] for i in queue:
        for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
     i queue for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
     nex v[i-1] v i-1 i 1 if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
     d[nex-1]==-1 d[nex-1] d nex-1 nex 1 -1 d[nex-1]=dis d[nex-1] d nex-1 nex 1 dis queue2.append(nex) queue2.append queue2 append nex dis +=1 dis 1 queue = queue2 queue queue2 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 n = int(input()) int(input()) n queue = [1] [1] queue v = [] [] v queue2 = [] [] queue2 dis +=1 1 dis dis = 1 1 dis d = [-1]*n [-1]*n d queue = queue2 queue2 queue v_i = list(map(int,input().split()))[2:] list(map(int,input().split()))[2:] v_i
n = int(input())
v = []
for i in range(n):
    v_i = list(map(int,input().split()))[2:]
    v.append(v_i)
    
d = [-1]*n
d[0]=0
queue = [1]
dis = 1
while len(queue)!=0:
    queue2 = []
    for i in queue:
        for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
    dis +=1
    queue = queue2

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