# 幅優先探索 Breadth first search: BFS
import queue

n = int(input())
Adj = [[] for i in range(n)]
for i in range(n):
    dat = [int(i) for i in input().split()]
    Adj[dat[0] - 1] = dat[2:]

D = [-1 for i in range(n)]
D[0] = 0
Q = queue.Queue()
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in Adj[u - 1]:
        if D[v - 1] == -1:
            Q.put(v)
            D[v - 1] = D[u - 1] + 1

for i in range(n):
    print(i + 1, D[i])
 import queue queue n = int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] Adj = [[] for i in range(n)] Adj [[] for i in range(n)] for i in range(n):
    dat = [int(i) for i in input().split()]
    Adj[dat[0] - 1] = dat[2:] i range(n) range n int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i dat = [int(i) for i in input().split()] dat [int(i) for i in input().split()] Adj[dat[0] - 1] = dat[2:] Adj[dat[0] - 1] Adj dat[0] - 1 dat[0] dat 0 1 dat[2:] dat 2: 2 -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 D = [-1 for i in range(n)] D [-1 for i in range(n)] D[0] = 0 D[0] D 0 0 Q = queue.Queue() Q queue.Queue() queue.Queue queue Queue Q.put(1) Q.put Q put 1 while not Q.empty():
    u = Q.get()
    for v in Adj[u - 1]:
        if D[v - 1] == -1:
            Q.put(v)
            D[v - 1] = D[u - 1] + 1 not Q.empty() Q.empty() Q.empty Q empty u = Q.get() u Q.get() Q.get Q get for v in Adj[u - 1]:
        if D[v - 1] == -1:
            Q.put(v)
            D[v - 1] = D[u - 1] + 1 v Adj[u - 1] Adj u - 1 u 1 if D[v - 1] == -1:
            Q.put(v)
            D[v - 1] = D[u - 1] + 1 D[v - 1] == -1 D[v - 1] D v - 1 v 1 -1 Q.put(v) Q.put Q put v D[v - 1] = D[u - 1] + 1 D[v - 1] D v - 1 v 1 D[u - 1] + 1 D[u - 1] D u - 1 u 1 1 for i in range(n):
    print(i + 1, D[i]) i range(n) range n print(i + 1, D[i]) print i + 1 i 1 D[i] D i n = int(input()) int(input()) n u = Q.get() Q.get() u dat = [int(i) for i in input().split()] [int(i) for i in input().split()] dat D = [-1 for i in range(n)] [-1 for i in range(n)] D Adj = [[] for i in range(n)] [[] for i in range(n)] Adj Q = queue.Queue() queue.Queue() Q
# 幅優先探索 Breadth first search: BFS
import queue

n = int(input())
Adj = [[] for i in range(n)]
for i in range(n):
    dat = [int(i) for i in input().split()]
    Adj[dat[0] - 1] = dat[2:]

D = [-1 for i in range(n)]
D[0] = 0
Q = queue.Queue()
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in Adj[u - 1]:
        if D[v - 1] == -1:
            Q.put(v)
            D[v - 1] = D[u - 1] + 1

for i in range(n):
    print(i + 1, D[i])

