#=============================================================================
# Breadth First Search
#=============================================================================
import queue

N=int(input())
Graph=[[] for _ in range(N+1)]
dist=[-1 for _ in range(N+1)]
for i in range(1,N+1):
    Graph[i]=list(map(int,input().split()))[2:]


que=queue.Queue()
que.put(1)
dist[1]=0
while not que.empty():
    v=que.get()
    for nv in Graph[v]:
        if dist[nv] != -1:
            continue
        dist[nv]=dist[v]+1
        que.put(nv)
for i in range(1,N+1):
    print(i,dist[i],sep=" ")
 import queue queue N=int(input()) N int(input()) int input() input [] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] Graph=[[] for _ in range(N+1)] Graph [[] for _ in range(N+1)] -1 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) -1 dist=[-1 for _ in range(N+1)] dist [-1 for _ in range(N+1)] for i in range(1,N+1):
    Graph[i]=list(map(int,input().split()))[2:] i range(1,N+1) range 1 N+1 N 1 Graph[i]=list(map(int,input().split()))[2:] Graph[i] Graph i list(map(int,input().split()))[2:] (map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 2: 2 que=queue.Queue() que queue.Queue() queue.Queue queue Queue que.put(1) que.put que put 1 dist[1]=0 dist[1] dist 1 0 while not que.empty():
    v=que.get()
    for nv in Graph[v]:
        if dist[nv] != -1:
            continue
        dist[nv]=dist[v]+1
        que.put(nv) not que.empty() que.empty() que.empty que empty v=que.get() v que.get() que.get que get for nv in Graph[v]:
        if dist[nv] != -1:
            continue
        dist[nv]=dist[v]+1
        que.put(nv) nv Graph[v] Graph v if dist[nv] != -1:
            continue
         dist[nv] != -1 dist[nv] dist nv -1 continue dist[nv]=dist[v]+1 dist[nv] dist nv dist[v]+1 dist[v] dist v 1 que.put(nv) que.put que put nv for i in range(1,N+1):
    print(i,dist[i],sep=" ") i range(1,N+1) range 1 N+1 N 1 print(i,dist[i],sep=" ") print i dist[i] dist i sep=" " sep " " dist=[-1 for _ in range(N+1)] [-1 for _ in range(N+1)] dist N=int(input()) int(input()) N que=queue.Queue() queue.Queue() que v=que.get() que.get() v Graph=[[] for _ in range(N+1)] [[] for _ in range(N+1)] Graph
#=============================================================================
# Breadth First Search
#=============================================================================
import queue

N=int(input())
Graph=[[] for _ in range(N+1)]
dist=[-1 for _ in range(N+1)]
for i in range(1,N+1):
    Graph[i]=list(map(int,input().split()))[2:]


que=queue.Queue()
que.put(1)
dist[1]=0
while not que.empty():
    v=que.get()
    for nv in Graph[v]:
        if dist[nv] != -1:
            continue
        dist[nv]=dist[v]+1
        que.put(nv)
for i in range(1,N+1):
    print(i,dist[i],sep=" ")

