from collections import deque
N=int(input())
color=["white"]*(N+1)
rinsetu=[[] for i in range(N+1)]
for i in range(N):
    a,*b=list(map(int,input().split()))
    for idx,j in enumerate(b):
        if idx==0:continue
        rinsetu[a].append(j)
distance=[10**5]*(N+1)
distance[1]=0
def bfs():
    d = deque()
    d.append(1)
    while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i])

bfs()
for i in range(1,N+1):
    if distance[i]==10**5:distance[i]=-1
    print(i,distance[i]) from collections import deque N=int(input()) N int(input()) int input() input color=["white"]*(N+1) color ["white"]*(N+1) ["white"] "white" N+1 N 1 [] for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [] rinsetu=[[] for i in range(N+1)] rinsetu [[] for i in range(N+1)] for i in range(N):
    a,*b=list(map(int,input().split()))
    for idx,j in enumerate(b):
        if idx==0:continue
        rinsetu[a].append(j) i range(N) range N a,*b=list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split *b b for idx,j in enumerate(b):
        if idx==0:continue
        rinsetu[a].append(j) idx j enumerate(b) enumerate b if idx==0:continue
         idx==0 idx 0 rinsetu[a].append(j) [a].append [a] rinsetu a append j distance=[10**5]*(N+1) distance [10**5]*(N+1) [10**5] 10**5 10 5 N+1 N 1 distance[1]=0 distance[1] distance 1 0 def bfs():
    d = deque()
    d.append(1)
    while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) bfs d = deque() d deque() deque d.append(1) d.append d append 1 while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) d num=d.popleft() num d.popleft() d.popleft d popleft color[num] = "black" color[num] color num "black" for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) i rinsetu[num] rinsetu num if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) color[i]=="white" color[i] color i "white" d.append(i) d.append d append i distance[i]=min(distance[num]+1,distance[i]) distance[i] distance i min(distance[num]+1,distance[i]) min distance[num]+1 distance[num] distance num 1 distance[i] distance i bfs() bfs for i in range(1,N+1):
    if distance[i]==10**5:distance[i]=-1
    print(i,distance[i]) i range(1,N+1) range 1 N+1 N 1 if distance[i]==10**5:distance[i]=-1
     distance[i]==10**5 distance[i] distance i 10**5 10 5 print(i,distance[i]) print i distance[i] distance i rinsetu=[[] for i in range(N+1)] [[] for i in range(N+1)] rinsetu a,*b=list(map(int,input().split())) list(map(int,input().split())) a N=int(input()) int(input()) N def bfs():
    d = deque()
    d.append(1)
    while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) def bfs():
    d = deque()
    d.append(1)
    while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i]) bfs color=["white"]*(N+1) ["white"]*(N+1) color distance=[10**5]*(N+1) [10**5]*(N+1) distance
from collections import deque
N=int(input())
color=["white"]*(N+1)
rinsetu=[[] for i in range(N+1)]
for i in range(N):
    a,*b=list(map(int,input().split()))
    for idx,j in enumerate(b):
        if idx==0:continue
        rinsetu[a].append(j)
distance=[10**5]*(N+1)
distance[1]=0
def bfs():
    d = deque()
    d.append(1)
    while(d):
        num=d.popleft()
        color[num] = "black"
        for i in rinsetu[num]:
            if color[i]=="white":
                d.append(i)
                distance[i]=min(distance[num]+1,distance[i])

bfs()
for i in range(1,N+1):
    if distance[i]==10**5:distance[i]=-1
    print(i,distance[i])
