n=int(input())
dic=[[] for i in range(n)]
for i in range(n):
    a=list(map(int,input().split()))
    a=a[2:]
    dic[i]=list(map(lambda x:x-1,a))

from collections import deque
d=deque()
d.append(0) #始点
completed=[False]*n
distance=[999]*n
distance[0]=0
while d:
    now=d.popleft()
    completed[now]=True
    for x in dic[now]: #次の頂点探し
        if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x)
for i in range(n):
    if distance[i]==999:
        distance[i]=-1
    print(i+1,distance[i])
     n=int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] dic=[[] for i in range(n)] dic [[] for i in range(n)] for i in range(n):
    a=list(map(int,input().split()))
    a=a[2:]
    dic[i]=list(map(lambda x:x-1,a)) i range(n) range n a=list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split a=a[2:] a a[2:] a 2: 2 dic[i]=list(map(lambda x:x-1,a)) dic[i] dic i list(map(lambda x:x-1,a)) list map(lambda x:x-1,a) map lambda x:x-1 x-1 x 1 x a from collections import deque d=deque() d deque() deque d.append(0) d.append d append 0 completed=[False]*n completed [False]*n [False] False n distance=[999]*n distance [999]*n [999] 999 n distance[0]=0 distance[0] distance 0 0 while d:
    now=d.popleft()
    completed[now]=True
    for x in dic[now]: #次の頂点探し
        if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x) d now=d.popleft() now d.popleft() d.popleft d popleft completed[now]=True completed[now] completed now True for x in dic[now]: #次の頂点探し
        if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x) x dic[now] dic now if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x) completed[x]==False completed[x] completed x False distance[x]=min(distance[now]+1,distance[x]) distance[x] distance x min(distance[now]+1,distance[x]) min distance[now]+1 distance[now] distance now 1 distance[x] distance x d.append(x) d.append d append x for i in range(n):
    if distance[i]==999:
        distance[i]=-1
    print(i+1,distance[i])
     i range(n) range n if distance[i]==999:
        distance[i]=-1
     distance[i]==999 distance[i] distance i 999 distance[i]=-1 distance[i] distance i -1 print(i+1,distance[i]) print i+1 i 1 distance[i] distance i distance=[999]*n [999]*n distance dic=[[] for i in range(n)] [[] for i in range(n)] dic d=deque() deque() d completed=[False]*n [False]*n completed now=d.popleft() d.popleft() now n=int(input()) int(input()) n a=list(map(int,input().split())) list(map(int,input().split())) a a=a[2:] a[2:] a
n=int(input())
dic=[[] for i in range(n)]
for i in range(n):
    a=list(map(int,input().split()))
    a=a[2:]
    dic[i]=list(map(lambda x:x-1,a))

from collections import deque
d=deque()
d.append(0) #始点
completed=[False]*n
distance=[999]*n
distance[0]=0
while d:
    now=d.popleft()
    completed[now]=True
    for x in dic[now]: #次の頂点探し
        if completed[x]==False: #その頂点に進んでもよいか
            distance[x]=min(distance[now]+1,distance[x])  #処理
            d.append(x)
for i in range(n):
    if distance[i]==999:
        distance[i]=-1
    print(i+1,distance[i])
    
