n=int(input())
G=[]
for i in range(n):
    u,k,*l=map(int,input().split())
    G.append(l)
#print(G)

#BFS
from collections import deque
def bfs(G,v,p):
    que=deque()
    que.append((v,p))
    log=[-1]*n
    log[0]=0
    while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
    return log

log=bfs(G,1,-1)
for i in enumerate(log,1):
    print(*i) n=int(input()) n int(input()) int input() input G=[] G [] for i in range(n):
    u,k,*l=map(int,input().split())
    G.append(l)
#print(G)

#BFS i range(n) range n u,k,*l=map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split k *l l G.append(l) G.append G append l from collections import deque def bfs(G,v,p):
    que=deque()
    que.append((v,p))
    log=[-1]*n
    log[0]=0
    while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
    return log bfs que=deque() que deque() deque que.append((v,p)) que.append que append (v,p) v p log=[-1]*n log [-1]*n [-1] -1 n log[0]=0 log[0] log 0 0 while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
     que V,P=que.popleft() V que.popleft() que.popleft que popleft P for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
     next_v G[V-1] G V-1 V 1 if log[next_v-1]!=-1:continue
             log[next_v-1]!=-1 log[next_v-1] log next_v-1 next_v 1 -1 log[next_v-1]=log[V-1]+1 log[next_v-1] log next_v-1 next_v 1 log[V-1]+1 log[V-1] log V-1 V 1 1 que.append((next_v,V)) que.append que append (next_v,V) next_v V return log log G G v v p p log=bfs(G,1,-1) log bfs(G,1,-1) bfs G 1 -1 for i in enumerate(log,1):
    print(*i) i enumerate(log,1) enumerate log 1 print(*i) print *i i log=bfs(G,1,-1) bfs(G,1,-1) log n=int(input()) int(input()) n k,*l=map(int,input().split()) map(int,input().split()) k G=[] [] G def bfs(G,v,p):
    que=deque()
    que.append((v,p))
    log=[-1]*n
    log[0]=0
    while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
    return log def bfs(G,v,p):
    que=deque()
    que.append((v,p))
    log=[-1]*n
    log[0]=0
    while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
    return log bfs u,k,*l=map(int,input().split()) map(int,input().split()) u
n=int(input())
G=[]
for i in range(n):
    u,k,*l=map(int,input().split())
    G.append(l)
#print(G)

#BFS
from collections import deque
def bfs(G,v,p):
    que=deque()
    que.append((v,p))
    log=[-1]*n
    log[0]=0
    while que:
        #queの先頭を取り出す
        V,P=que.popleft()
        for next_v in G[V-1]:
            if log[next_v-1]!=-1:continue
            log[next_v-1]=log[V-1]+1
            que.append((next_v,V))
    return log

log=bfs(G,1,-1)
for i in enumerate(log,1):
    print(*i)
