from collections import deque
n=int(input())
l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)]

flg=["white"]*(n+1)
dist=[-1]*(n+1)

def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black"

BFS(1)

for num,i in enumerate(dist[1:],1):
    print(num,i) from collections import deque n=int(input()) n int(input()) int input() input list(map(int,input().split()))[2:] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list(map(int,input().split()))[2:] (map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 2: 2 l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)] l [0]+[list(map(int,input().split()))[2:] for _ in range(n)] [0] 0 [list(map(int,input().split()))[2:] for _ in range(n)] flg=["white"]*(n+1) flg ["white"]*(n+1) ["white"] "white" n+1 n 1 dist=[-1]*(n+1) dist [-1]*(n+1) [-1] -1 n+1 n 1 def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black" BFS queue = deque([x]) queue deque([x]) deque [x] x dist[x]=0 dist[x] dist x 0 flg[x]="gray" flg[x] flg x "gray" while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black" queue current=queue.popleft() current queue.popleft() queue.popleft queue popleft for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
         i l[current] l current if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
         flg[i]=="white" flg[i] flg i "white" queue.append(i) queue.append queue append i flg[i]="gray" flg[i] flg i "gray" dist[i]=dist[current]+1 dist[i] dist i dist[current]+1 dist[current] dist current 1 flg[current]="black" flg[current] flg current "black" x x BFS(1) BFS 1 for num,i in enumerate(dist[1:],1):
    print(num,i) num i enumerate(dist[1:],1) enumerate dist[1:] dist 1: 1 1 print(num,i) print num i n=int(input()) int(input()) n dist=[-1]*(n+1) [-1]*(n+1) dist flg=["white"]*(n+1) ["white"]*(n+1) flg l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)] [0]+[list(map(int,input().split()))[2:] for _ in range(n)] l def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black" def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black" BFS
from collections import deque
n=int(input())
l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)]

flg=["white"]*(n+1)
dist=[-1]*(n+1)

def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black"

BFS(1)

for num,i in enumerate(dist[1:],1):
    print(num,i)
