from collections import deque

N = int(input())
nodes = []
G ={}
is_visited = {}
q = deque()

for _ in range(N):
    lst = list(map(int, input().split()))
    idx = lst[0]
    nodes.append(idx)
    is_visited[idx] = False
    degree = lst[1]
    if degree > 0:
        G[idx] = lst[2:]
    else:
        G[idx] = []

INF = 10**12
costs = [INF]*N
# bfs
# 初期queue
que = deque()  # 必要なものはnodeidと原点からの距離
que.append(1)
costs[0] = 0
while que:
    node_id = que.popleft()  # 先入れ先出し
    cost = costs[node_id-1]
    for next_id in G[node_id]:
        if (costs[next_id-1] == INF):
            que.append(next_id)
            costs[next_id-1] = cost + 1
        
for i in range(N):
    if costs[i] != INF:
        print(i+1, costs[i])
    else:
        print(i+1, -1) from collections import deque N = int(input()) N int(input()) int input() input nodes = [] nodes [] G ={} G {} is_visited = {} is_visited {} q = deque() q deque() deque for _ in range(N):
    lst = list(map(int, input().split()))
    idx = lst[0]
    nodes.append(idx)
    is_visited[idx] = False
    degree = lst[1]
    if degree > 0:
        G[idx] = lst[2:]
    else:
        G[idx] = [] _ range(N) range N lst = list(map(int, input().split())) lst list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split idx = lst[0] idx lst[0] lst 0 nodes.append(idx) nodes.append nodes append idx is_visited[idx] = False is_visited[idx] is_visited idx False degree = lst[1] degree lst[1] lst 1 if degree > 0:
        G[idx] = lst[2:]
    else:
        G[idx] = [] degree > 0 degree 0 G[idx] = lst[2:] G[idx] G idx lst[2:] lst 2: 2 G[idx] = [] G[idx] G idx [] INF = 10**12 INF 10**12 10 12 costs = [INF]*N costs [INF]*N [INF] INF N que = deque() que deque() deque que.append(1) que.append que append 1 costs[0] = 0 costs[0] costs 0 0 while que:
    node_id = que.popleft()  # 先入れ先出し
    cost = costs[node_id-1]
    for next_id in G[node_id]:
        if (costs[next_id-1] == INF):
            que.append(next_id)
            costs[next_id-1] = cost + 1
         que node_id = que.popleft() node_id que.popleft() que.popleft que popleft cost = costs[node_id-1] cost costs[node_id-1] costs node_id-1 node_id 1 for next_id in G[node_id]:
        if (costs[next_id-1] == INF):
            que.append(next_id)
            costs[next_id-1] = cost + 1
         next_id G[node_id] G node_id if (costs[next_id-1] == INF):
            que.append(next_id)
            costs[next_id-1] = cost + 1
         costs[next_id-1] == INF costs[next_id-1] costs next_id-1 next_id 1 INF que.append(next_id) que.append que append next_id costs[next_id-1] = cost + 1 costs[next_id-1] costs next_id-1 next_id 1 cost + 1 cost 1 for i in range(N):
    if costs[i] != INF:
        print(i+1, costs[i])
    else:
        print(i+1, -1) i range(N) range N if costs[i] != INF:
        print(i+1, costs[i])
    else:
        print(i+1, -1) costs[i] != INF costs[i] costs i INF print(i+1, costs[i]) print i+1 i 1 costs[i] costs i print(i+1, -1) print i+1 i 1 -1 nodes = [] [] nodes is_visited = {} {} is_visited INF = 10**12 10**12 INF lst = list(map(int, input().split())) list(map(int, input().split())) lst node_id = que.popleft() que.popleft() node_id cost = costs[node_id-1] costs[node_id-1] cost degree = lst[1] lst[1] degree costs = [INF]*N [INF]*N costs idx = lst[0] lst[0] idx que = deque() deque() que N = int(input()) int(input()) N G ={} {} G q = deque() deque() q
from collections import deque

N = int(input())
nodes = []
G ={}
is_visited = {}
q = deque()

for _ in range(N):
    lst = list(map(int, input().split()))
    idx = lst[0]
    nodes.append(idx)
    is_visited[idx] = False
    degree = lst[1]
    if degree > 0:
        G[idx] = lst[2:]
    else:
        G[idx] = []

INF = 10**12
costs = [INF]*N
# bfs
# 初期queue
que = deque()  # 必要なものはnodeidと原点からの距離
que.append(1)
costs[0] = 0
while que:
    node_id = que.popleft()  # 先入れ先出し
    cost = costs[node_id-1]
    for next_id in G[node_id]:
        if (costs[next_id-1] == INF):
            que.append(next_id)
            costs[next_id-1] = cost + 1
        
for i in range(N):
    if costs[i] != INF:
        print(i+1, costs[i])
    else:
        print(i+1, -1)
