n = int(input())
adjlis = []
for _ in range(n):
    lis=list(map(int,input().split()))
    adjlis.append(lis[2:])

visited = [False]*n
dislis = [n+1]*n
Q = [[1,0]]

def pull():
    return Q.pop(0)

def push(x):
    Q.append(x)

while len(Q)>0:
    cur = pull()
    curpoint = cur[0]
    count = cur[1]
    if not visited[curpoint-1]:
        visited[curpoint-1]=True
        dislis[curpoint-1] = count
        for i in adjlis[curpoint-1]:
            push([i,count+1])


for i in range(n):
    if dislis[i]==n+1:
        dislis[i] = -1
    print(i+1,dislis[i])
             n = int(input()) n int(input()) int input() input adjlis = [] adjlis [] for _ in range(n):
    lis=list(map(int,input().split()))
    adjlis.append(lis[2:]) _ range(n) range n lis=list(map(int,input().split())) lis list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split adjlis.append(lis[2:]) adjlis.append adjlis append lis[2:] lis 2: 2 visited = [False]*n visited [False]*n [False] False n dislis = [n+1]*n dislis [n+1]*n [n+1] n+1 n 1 n Q = [[1,0]] Q [[1,0]] [1,0] 1 0 def pull():
    return Q.pop(0) pull return Q.pop(0) Q.pop(0) Q.pop Q pop 0 def push(x):
    Q.append(x) push Q.append(x) Q.append Q append x x x while len(Q)>0:
    cur = pull()
    curpoint = cur[0]
    count = cur[1]
    if not visited[curpoint-1]:
        visited[curpoint-1]=True
        dislis[curpoint-1] = count
        for i in adjlis[curpoint-1]:
            push([i,count+1]) len(Q)>0 len(Q) len Q 0 cur = pull() cur pull() pull curpoint = cur[0] curpoint cur[0] cur 0 count = cur[1] count cur[1] cur 1 if not visited[curpoint-1]:
        visited[curpoint-1]=True
        dislis[curpoint-1] = count
        for i in adjlis[curpoint-1]:
            push([i,count+1]) not visited[curpoint-1] visited[curpoint-1] visited curpoint-1 curpoint 1 visited[curpoint-1]=True visited[curpoint-1] visited curpoint-1 curpoint 1 True dislis[curpoint-1] = count dislis[curpoint-1] dislis curpoint-1 curpoint 1 count for i in adjlis[curpoint-1]:
            push([i,count+1]) i adjlis[curpoint-1] adjlis curpoint-1 curpoint 1 push([i,count+1]) push [i,count+1] i count+1 count 1 for i in range(n):
    if dislis[i]==n+1:
        dislis[i] = -1
    print(i+1,dislis[i])
             i range(n) range n if dislis[i]==n+1:
        dislis[i] = -1
     dislis[i]==n+1 dislis[i] dislis i n+1 n 1 dislis[i] = -1 dislis[i] dislis i -1 print(i+1,dislis[i]) print i+1 i 1 dislis[i] dislis i n = int(input()) int(input()) n adjlis = [] [] adjlis def push(x):
    Q.append(x) def push(x):
    Q.append(x) push lis=list(map(int,input().split())) list(map(int,input().split())) lis dislis = [n+1]*n [n+1]*n dislis curpoint = cur[0] cur[0] curpoint count = cur[1] cur[1] count visited = [False]*n [False]*n visited def pull():
    return Q.pop(0) def pull():
    return Q.pop(0) pull cur = pull() pull() cur Q = [[1,0]] [[1,0]] Q
n = int(input())
adjlis = []
for _ in range(n):
    lis=list(map(int,input().split()))
    adjlis.append(lis[2:])

visited = [False]*n
dislis = [n+1]*n
Q = [[1,0]]

def pull():
    return Q.pop(0)

def push(x):
    Q.append(x)

while len(Q)>0:
    cur = pull()
    curpoint = cur[0]
    count = cur[1]
    if not visited[curpoint-1]:
        visited[curpoint-1]=True
        dislis[curpoint-1] = count
        for i in adjlis[curpoint-1]:
            push([i,count+1])


for i in range(n):
    if dislis[i]==n+1:
        dislis[i] = -1
    print(i+1,dislis[i])
            
