#coding:utf-8
                
def BFS(i,A,d,dist):
    dist[i]=d
    for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist)

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
    dist=[-1]*n
    BFS(0,A,0,dist)

    for i in range(n):
        print("{} {}".format(i+1,dist[i])) def BFS(i,A,d,dist):
    dist[i]=d
    for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist) BFS dist[i]=d dist[i] dist i d for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist) point A[i] A i if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist) dist[point-1]==-1 or dist[point-1]>d+1 dist[point-1]==-1 dist[point-1] dist point-1 point 1 -1 dist[point-1]>d+1 dist[point-1] dist point-1 point 1 d+1 d 1 BFS(point-1,A,d+1,dist) BFS point-1 point 1 A d+1 d 1 dist i i A A d d dist dist if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
    dist=[-1]*n
    BFS(0,A,0,dist)

    for i in range(n):
        print("{} {}".format(i+1,dist[i])) __name__=="__main__" __name__ "__main__" n=int(input()) n int(input()) int input() input A=[] A [] for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
     i range(n) range n A.append(list(map(int,input().split(" ")))[2:]) A.append A append list(map(int,input().split(" ")))[2:] (map(int,input().split(" "))) list map(int,input().split(" ")) map int input().split(" ") ().split () input split " " 2: 2 dist=[-1]*n dist [-1]*n [-1] -1 n BFS(0,A,0,dist) BFS 0 A 0 dist for i in range(n):
        print("{} {}".format(i+1,dist[i])) i range(n) range n print("{} {}".format(i+1,dist[i])) print "{} {}".format(i+1,dist[i]) "{} {}".format "{} {}" format i+1 i 1 dist[i] dist i def BFS(i,A,d,dist):
    dist[i]=d
    for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist) def BFS(i,A,d,dist):
    dist[i]=d
    for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist) BFS A=[] [] A dist=[-1]*n [-1]*n dist n=int(input()) int(input()) n
#coding:utf-8
                
def BFS(i,A,d,dist):
    dist[i]=d
    for point in A[i]:
        if dist[point-1]==-1 or dist[point-1]>d+1:
            BFS(point-1,A,d+1,dist)

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" ")))[2:])
    dist=[-1]*n
    BFS(0,A,0,dist)

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