# ALDS1_11_C.py
import queue


def printNode():
    for i, node in enumerate(glaph):
        print(i, node)


N = int(input())
glaph = [[] for i in range(N+1)]
for i in range(1, N+1):
    glaph[i] = list(map(int, input().split()))[2:]
# printNode()

step = [-1]*(N+1)
step[1] = 0

q = queue.Queue()
q.put(1)
while not q.empty():
    now = q.get()
    next_node = glaph[now]
#    print("now is ", now, ", next is", next_node)
    for i in next_node:
        if step[i] != -1:
            continue
        q.put(i)
        step[i] = step[now] + 1

for i, v in enumerate(step):
    if i != 0:
        print(i, v)
 import queue queue def printNode():
    for i, node in enumerate(glaph):
        print(i, node) printNode for i, node in enumerate(glaph):
        print(i, node) i node enumerate(glaph) enumerate glaph print(i, node) print i node N = int(input()) N int(input()) int input() input [] for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [] glaph = [[] for i in range(N+1)] glaph [[] for i in range(N+1)] for i in range(1, N+1):
    glaph[i] = list(map(int, input().split()))[2:]
# printNode() i range(1, N+1) range 1 N+1 N 1 glaph[i] = list(map(int, input().split()))[2:] glaph[i] glaph 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 step = [-1]*(N+1) step [-1]*(N+1) [-1] -1 N+1 N 1 step[1] = 0 step[1] step 1 0 q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(1) q.put q put 1 while not q.empty():
    now = q.get()
    next_node = glaph[now]
#    print("now is ", now, ", next is", next_node)
    for i in next_node:
        if step[i] != -1:
            continue
        q.put(i)
        step[i] = step[now] + 1 not q.empty() q.empty() q.empty q empty now = q.get() now q.get() q.get q get next_node = glaph[now] next_node glaph[now] glaph now for i in next_node:
        if step[i] != -1:
            continue
        q.put(i)
        step[i] = step[now] + 1 i next_node if step[i] != -1:
            continue
         step[i] != -1 step[i] step i -1 continue q.put(i) q.put q put i step[i] = step[now] + 1 step[i] step i step[now] + 1 step[now] step now 1 for i, v in enumerate(step):
    if i != 0:
        print(i, v) i v enumerate(step) enumerate step if i != 0:
        print(i, v) i != 0 i 0 print(i, v) print i v glaph = [[] for i in range(N+1)] [[] for i in range(N+1)] glaph N = int(input()) int(input()) N step = [-1]*(N+1) [-1]*(N+1) step def printNode():
    for i, node in enumerate(glaph):
        print(i, node) def printNode():
    for i, node in enumerate(glaph):
        print(i, node) printNode q = queue.Queue() queue.Queue() q next_node = glaph[now] glaph[now] next_node now = q.get() q.get() now
# ALDS1_11_C.py
import queue


def printNode():
    for i, node in enumerate(glaph):
        print(i, node)


N = int(input())
glaph = [[] for i in range(N+1)]
for i in range(1, N+1):
    glaph[i] = list(map(int, input().split()))[2:]
# printNode()

step = [-1]*(N+1)
step[1] = 0

q = queue.Queue()
q.put(1)
while not q.empty():
    now = q.get()
    next_node = glaph[now]
#    print("now is ", now, ", next is", next_node)
    for i in next_node:
        if step[i] != -1:
            continue
        q.put(i)
        step[i] = step[now] + 1

for i, v in enumerate(step):
    if i != 0:
        print(i, v)

