#木ではない
#非連結成分
#処理済みの判定、辺の距離はすべて1なので一度到達すればよい
from collections import deque

n = int(input())
V = [[] for _ in range(n+1)]
for _ in range(n):
    inpt = list(map(int,input().split( )))
    if len(inpt)>2:
        V[inpt[0]] = inpt[2:]

lrg = 10000
D = [lrg]*(n+1)
End = [0]*(n+1)

D[1] = 0
Deq = deque()

time = 1
Q = [1]
Deq.append(1)

while len(Deq) > 0:
    v = Deq.popleft()
    for i in V[v]:
        D[i] = min(D[i],D[v]+1)
        if End[i] == 0:
            Deq.append(i)
        End[i] = 1


for i in range(1,n+1):
    if D[i]<lrg-1:
        print(i,D[i])
    else:
        print(i,-1)
 from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] V = [[] for _ in range(n+1)] V [[] for _ in range(n+1)] for _ in range(n):
    inpt = list(map(int,input().split( )))
    if len(inpt)>2:
        V[inpt[0]] = inpt[2:] _ range(n) range n inpt = list(map(int,input().split( ))) inpt list(map(int,input().split( ))) list map(int,input().split( )) map int input().split( ) ().split () input split if len(inpt)>2:
        V[inpt[0]] = inpt[2:] len(inpt)>2 len(inpt) len inpt 2 V[inpt[0]] = inpt[2:] V[inpt[0]] V inpt[0] inpt 0 inpt[2:] inpt 2: 2 lrg = 10000 lrg 10000 D = [lrg]*(n+1) D [lrg]*(n+1) [lrg] lrg n+1 n 1 End = [0]*(n+1) End [0]*(n+1) [0] 0 n+1 n 1 D[1] = 0 D[1] D 1 0 Deq = deque() Deq deque() deque time = 1 time 1 Q = [1] Q [1] 1 Deq.append(1) Deq.append Deq append 1 while len(Deq) > 0:
    v = Deq.popleft()
    for i in V[v]:
        D[i] = min(D[i],D[v]+1)
        if End[i] == 0:
            Deq.append(i)
        End[i] = 1 len(Deq) > 0 len(Deq) len Deq 0 v = Deq.popleft() v Deq.popleft() Deq.popleft Deq popleft for i in V[v]:
        D[i] = min(D[i],D[v]+1)
        if End[i] == 0:
            Deq.append(i)
        End[i] = 1 i V[v] V v D[i] = min(D[i],D[v]+1) D[i] D i min(D[i],D[v]+1) min D[i] D i D[v]+1 D[v] D v 1 if End[i] == 0:
            Deq.append(i)
         End[i] == 0 End[i] End i 0 Deq.append(i) Deq.append Deq append i End[i] = 1 End[i] End i 1 for i in range(1,n+1):
    if D[i]<lrg-1:
        print(i,D[i])
    else:
        print(i,-1) i range(1,n+1) range 1 n+1 n 1 if D[i]<lrg-1:
        print(i,D[i])
    else:
        print(i,-1) D[i]<lrg-1 D[i] D i lrg-1 lrg 1 print(i,D[i]) print i D[i] D i print(i,-1) print i -1 time = 1 1 time Q = [1] [1] Q v = Deq.popleft() Deq.popleft() v D = [lrg]*(n+1) [lrg]*(n+1) D End = [0]*(n+1) [0]*(n+1) End Deq = deque() deque() Deq inpt = list(map(int,input().split( ))) list(map(int,input().split( ))) inpt lrg = 10000 10000 lrg n = int(input()) int(input()) n V = [[] for _ in range(n+1)] [[] for _ in range(n+1)] V
#木ではない
#非連結成分
#処理済みの判定、辺の距離はすべて1なので一度到達すればよい
from collections import deque

n = int(input())
V = [[] for _ in range(n+1)]
for _ in range(n):
    inpt = list(map(int,input().split( )))
    if len(inpt)>2:
        V[inpt[0]] = inpt[2:]

lrg = 10000
D = [lrg]*(n+1)
End = [0]*(n+1)

D[1] = 0
Deq = deque()

time = 1
Q = [1]
Deq.append(1)

while len(Deq) > 0:
    v = Deq.popleft()
    for i in V[v]:
        D[i] = min(D[i],D[v]+1)
        if End[i] == 0:
            Deq.append(i)
        End[i] = 1


for i in range(1,n+1):
    if D[i]<lrg-1:
        print(i,D[i])
    else:
        print(i,-1)

