N=int(input())
List = [[] for i in range(N+1)]
for i in range(N):
    l=list(map(int,input().split()))
    List[l[0]]=l[2:]
#print(List)
D=[10**9 for i in range(N+1)]

Q=[[0,1]]
for i in range(10**9):
    if len(Q)==i:
        break
    if D[Q[i][1]]==10**9:
        D[Q[i][1]]=Q[i][0]
        for j in List[Q[i][1]]:
            if D[j]==10**9:
                Q.append([Q[i][0]+1,j])
for i in range(1,N+1):
    if D[i]==10**9:
        print(i,-1)
    else:
        print(i,D[i]) N=int(input()) N int(input()) int input() input [] 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) [] List = [[] for i in range(N+1)] List [[] for i in range(N+1)] for i in range(N):
    l=list(map(int,input().split()))
    List[l[0]]=l[2:]
#print(List) i range(N) range N l=list(map(int,input().split())) l list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split List[l[0]]=l[2:] List[l[0]] List l[0] l 0 l[2:] l 2: 2 10**9 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) 10**9 10 9 D=[10**9 for i in range(N+1)] D [10**9 for i in range(N+1)] Q=[[0,1]] Q [[0,1]] [0,1] 0 1 for i in range(10**9):
    if len(Q)==i:
        break
    if D[Q[i][1]]==10**9:
        D[Q[i][1]]=Q[i][0]
        for j in List[Q[i][1]]:
            if D[j]==10**9:
                Q.append([Q[i][0]+1,j]) i range(10**9) range 10**9 10 9 if len(Q)==i:
        break
     len(Q)==i len(Q) len Q i break if D[Q[i][1]]==10**9:
        D[Q[i][1]]=Q[i][0]
        for j in List[Q[i][1]]:
            if D[j]==10**9:
                Q.append([Q[i][0]+1,j]) D[Q[i][1]]==10**9 D[Q[i][1]] D Q[i][1] [i] Q i 1 10**9 10 9 D[Q[i][1]]=Q[i][0] D[Q[i][1]] D Q[i][1] [i] Q i 1 Q[i][0] [i] Q i 0 for j in List[Q[i][1]]:
            if D[j]==10**9:
                Q.append([Q[i][0]+1,j]) j List[Q[i][1]] List Q[i][1] [i] Q i 1 if D[j]==10**9:
                Q.append([Q[i][0]+1,j]) D[j]==10**9 D[j] D j 10**9 10 9 Q.append([Q[i][0]+1,j]) Q.append Q append [Q[i][0]+1,j] Q[i][0]+1 Q[i][0] [i] Q i 0 1 j for i in range(1,N+1):
    if D[i]==10**9:
        print(i,-1)
    else:
        print(i,D[i]) i range(1,N+1) range 1 N+1 N 1 if D[i]==10**9:
        print(i,-1)
    else:
        print(i,D[i]) D[i]==10**9 D[i] D i 10**9 10 9 print(i,-1) print i -1 print(i,D[i]) print i D[i] D i D=[10**9 for i in range(N+1)] [10**9 for i in range(N+1)] D List = [[] for i in range(N+1)] [[] for i in range(N+1)] List Q=[[0,1]] [[0,1]] Q N=int(input()) int(input()) N l=list(map(int,input().split())) list(map(int,input().split())) l
N=int(input())
List = [[] for i in range(N+1)]
for i in range(N):
    l=list(map(int,input().split()))
    List[l[0]]=l[2:]
#print(List)
D=[10**9 for i in range(N+1)]

Q=[[0,1]]
for i in range(10**9):
    if len(Q)==i:
        break
    if D[Q[i][1]]==10**9:
        D[Q[i][1]]=Q[i][0]
        for j in List[Q[i][1]]:
            if D[j]==10**9:
                Q.append([Q[i][0]+1,j])
for i in range(1,N+1):
    if D[i]==10**9:
        print(i,-1)
    else:
        print(i,D[i])
