n=int(input())

lst=[0]*n

for _ in range(n):
    work=list(map(int,input().split()))
    lst[work[0]-1]=work[2:]

dis=[-1]*n
dis[0]=0
work=lst[0][:]
work_next=[]
token=0

while work:
    token+=1
    for x in work:
        if dis[x-1]==-1: dis[x-1]=token
        for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
    work=work_next[:]
    work_next=[]

for i in range(n):
    print("{} {}".format(i+1,dis[i])) n=int(input()) n int(input()) int input() input lst=[0]*n lst [0]*n [0] 0 n for _ in range(n):
    work=list(map(int,input().split()))
    lst[work[0]-1]=work[2:] _ range(n) range n work=list(map(int,input().split())) work list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split lst[work[0]-1]=work[2:] lst[work[0]-1] lst work[0]-1 work[0] work 0 1 work[2:] work 2: 2 dis=[-1]*n dis [-1]*n [-1] -1 n dis[0]=0 dis[0] dis 0 0 work=lst[0][:] work lst[0][:] [0] lst 0 : work_next=[] work_next [] token=0 token 0 while work:
    token+=1
    for x in work:
        if dis[x-1]==-1: dis[x-1]=token
        for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
    work=work_next[:]
    work_next=[] work token+=1 token 1 for x in work:
        if dis[x-1]==-1: dis[x-1]=token
        for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
     x work if dis[x-1]==-1: dis[x-1]=token
         dis[x-1]==-1 dis[x-1] dis x-1 x 1 -1 for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
     y lst[x-1] lst x-1 x 1 if dis[y-1]==-1 : work_next.append(y)
     dis[y-1]==-1 dis[y-1] dis y-1 y 1 -1 work=work_next[:] work work_next[:] work_next : work_next=[] work_next [] for i in range(n):
    print("{} {}".format(i+1,dis[i])) i range(n) range n print("{} {}".format(i+1,dis[i])) print "{} {}".format(i+1,dis[i]) "{} {}".format "{} {}" format i+1 i 1 dis[i] dis i work=work_next[:] work_next[:] work token=0 0 token work_next=[] [] work_next token+=1 1 token work_next=[] [] work_next n=int(input()) int(input()) n lst=[0]*n [0]*n lst work=lst[0][:] lst[0][:] work dis=[-1]*n [-1]*n dis work=list(map(int,input().split())) list(map(int,input().split())) work
n=int(input())

lst=[0]*n

for _ in range(n):
    work=list(map(int,input().split()))
    lst[work[0]-1]=work[2:]

dis=[-1]*n
dis[0]=0
work=lst[0][:]
work_next=[]
token=0

while work:
    token+=1
    for x in work:
        if dis[x-1]==-1: dis[x-1]=token
        for y in lst[x-1]:
            if dis[y-1]==-1 : work_next.append(y)
    work=work_next[:]
    work_next=[]

for i in range(n):
    print("{} {}".format(i+1,dis[i]))
