def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:]


if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out) def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:] BFS [i+1,-1] for i in range(len(G)) for i in range(len(G)) i range(len(G)) range len(G) len G for i in range(len(G)) [i+1,-1] i+1 i 1 -1 ans = [0] + [[i+1,-1] for i in range(len(G))] ans [0] + [[i+1,-1] for i in range(len(G))] [0] 0 [[i+1,-1] for i in range(len(G))] def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
     main if -1 < ans[i][1] <= d: return
         -1 < ans[i][1] <= d -1 < ans[i][1] -1 ans[i][1] [i] ans i 1 d ans[i] = [i, d] ans[i] ans i [i, d] i d for j in G[i-1][2:]: main(G,j,d+1)
     j G[i-1][2:] [i-1] G i-1 i 1 2: 2 G G i i d d main(G,1,0) main G 1 0 return ans[1:] ans[1:] ans 1: 1 G G if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out) __name__=='__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input list(map(int,input().split())) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split G = [list(map(int,input().split())) for _ in range(n)] G [list(map(int,input().split())) for _ in range(n)] for out in BFS(G): print(*out) out BFS(G) BFS G n = int(input()) int(input()) n def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:] def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:] BFS G = [list(map(int,input().split())) for _ in range(n)] [list(map(int,input().split())) for _ in range(n)] G
def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        if -1 < ans[i][1] <= d: return
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:]


if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out)