def get_depth(graph,tmp_depth,vertex_list,depth_list):
    new_vertex_list=[]
    for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
    if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成
n=int(input())
graph=[[0]*n for loop in range(n)]
for loop in range(n):
    tmp_ope=list(map(int,input().split()))
    for j in range(tmp_ope[1]):
        graph[tmp_ope[0]-1][tmp_ope[j+2]-1]=1


depth_list=[-1]*n
depth_list[0]=0
vertex_list=[0]
get_depth(graph,0,vertex_list,depth_list)

for i in range(n):
    print(f"{i+1} {depth_list[i]}") def get_depth(graph,tmp_depth,vertex_list,depth_list):
    new_vertex_list=[]
    for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
    if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成 get_depth new_vertex_list=[] new_vertex_list [] for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
     vertex vertex_list for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
     j range(len(depth_list)) range len(depth_list) len depth_list if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
     graph[vertex][j]!=0 and depth_list[j]==-1 graph[vertex][j]!=0 graph[vertex][j] [vertex] graph vertex j 0 depth_list[j]==-1 depth_list[j] depth_list j -1 depth_list[j]=tmp_depth + 1 depth_list[j] depth_list j tmp_depth + 1 tmp_depth 1 new_vertex_list.append(j) new_vertex_list.append new_vertex_list append j if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成 len(new_vertex_list)!=0 len(new_vertex_list) len new_vertex_list 0 get_depth(graph,tmp_depth+1,new_vertex_list,depth_list) get_depth graph tmp_depth+1 tmp_depth 1 new_vertex_list depth_list graph graph tmp_depth tmp_depth vertex_list vertex_list depth_list depth_list n=int(input()) n int(input()) int input() input [0]*n for loop in range(n) for loop in range(n) loop range(n) range n for loop in range(n) [0]*n [0] 0 n graph=[[0]*n for loop in range(n)] graph [[0]*n for loop in range(n)] for loop in range(n):
    tmp_ope=list(map(int,input().split()))
    for j in range(tmp_ope[1]):
        graph[tmp_ope[0]-1][tmp_ope[j+2]-1]=1 loop range(n) range n tmp_ope=list(map(int,input().split())) tmp_ope list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split for j in range(tmp_ope[1]):
        graph[tmp_ope[0]-1][tmp_ope[j+2]-1]=1 j range(tmp_ope[1]) range tmp_ope[1] tmp_ope 1 graph[tmp_ope[0]-1][tmp_ope[j+2]-1]=1 graph[tmp_ope[0]-1][tmp_ope[j+2]-1] [tmp_ope[0]-1] graph tmp_ope[0]-1 tmp_ope[0] tmp_ope 0 1 tmp_ope[j+2]-1 tmp_ope[j+2] tmp_ope j+2 j 2 1 1 depth_list=[-1]*n depth_list [-1]*n [-1] -1 n depth_list[0]=0 depth_list[0] depth_list 0 0 vertex_list=[0] vertex_list [0] 0 get_depth(graph,0,vertex_list,depth_list) get_depth graph 0 vertex_list depth_list for i in range(n):
    print(f"{i+1} {depth_list[i]}") i range(n) range n print(f"{i+1} {depth_list[i]}") print f"{i+1} {depth_list[i]}" n=int(input()) int(input()) n depth_list=[-1]*n [-1]*n depth_list vertex_list=[0] [0] vertex_list def get_depth(graph,tmp_depth,vertex_list,depth_list):
    new_vertex_list=[]
    for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
    if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成 def get_depth(graph,tmp_depth,vertex_list,depth_list):
    new_vertex_list=[]
    for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
    if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成 get_depth tmp_ope=list(map(int,input().split())) list(map(int,input().split())) tmp_ope graph=[[0]*n for loop in range(n)] [[0]*n for loop in range(n)] graph
def get_depth(graph,tmp_depth,vertex_list,depth_list):
    new_vertex_list=[]
    for vertex in vertex_list:
        for j in range(len(depth_list)):
            if(graph[vertex][j]!=0 and depth_list[j]==-1):
                depth_list[j]=tmp_depth + 1
                new_vertex_list.append(j)
    if(len(new_vertex_list)!=0):
        get_depth(graph,tmp_depth+1,new_vertex_list,depth_list)


#グラフの作成
n=int(input())
graph=[[0]*n for loop in range(n)]
for loop in range(n):
    tmp_ope=list(map(int,input().split()))
    for j in range(tmp_ope[1]):
        graph[tmp_ope[0]-1][tmp_ope[j+2]-1]=1


depth_list=[-1]*n
depth_list[0]=0
vertex_list=[0]
get_depth(graph,0,vertex_list,depth_list)

for i in range(n):
    print(f"{i+1} {depth_list[i]}")
