from collections import deque

n = int(input())
g = [list(map(int, input().split()))[2:] for _ in range(n)]
que = deque([0])
dist = [-1] * n
dist[0] = 0  # 初期条件
while que:  # queが空なるまで
    u = que.popleft()  # queの左から順に取り出す
    for v in g[u]:
        v -= 1  # 0-indexedに変更
        if dist[v] == -1:  # 未探索の時
            dist[v] = dist[u] + 1  # 最短距離を記録
            que.append(v)  # queに追加
for i in range(n):
    print(i + 1, dist[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 g = [list(map(int, input().split()))[2:] for _ in range(n)] g [list(map(int, input().split()))[2:] for _ in range(n)] que = deque([0]) que deque([0]) deque [0] 0 dist = [-1] * n dist [-1] * n [-1] -1 n dist[0] = 0 dist[0] dist 0 0 while que:  # queが空なるまで
    u = que.popleft()  # queの左から順に取り出す
    for v in g[u]:
        v -= 1  # 0-indexedに変更
        if dist[v] == -1:  # 未探索の時
            dist[v] = dist[u] + 1  # 最短距離を記録
            que.append(v)  # queに追加 que u = que.popleft() u que.popleft() que.popleft que popleft for v in g[u]:
        v -= 1  # 0-indexedに変更
        if dist[v] == -1:  # 未探索の時
            dist[v] = dist[u] + 1  # 最短距離を記録
            que.append(v)  # queに追加 v g[u] g u v -= 1 v 1 if dist[v] == -1:  # 未探索の時
            dist[v] = dist[u] + 1  # 最短距離を記録
            que.append(v)  # queに追加 dist[v] == -1 dist[v] dist v -1 dist[v] = dist[u] + 1 dist[v] dist v dist[u] + 1 dist[u] dist u 1 que.append(v) que.append que append v for i in range(n):
    print(i + 1, dist[i]) i range(n) range n print(i + 1, dist[i]) print i + 1 i 1 dist[i] dist i u = que.popleft() que.popleft() u que = deque([0]) deque([0]) que dist = [-1] * n [-1] * n dist v -= 1 1 v g = [list(map(int, input().split()))[2:] for _ in range(n)] [list(map(int, input().split()))[2:] for _ in range(n)] g n = int(input()) int(input()) n
from collections import deque

n = int(input())
g = [list(map(int, input().split()))[2:] for _ in range(n)]
que = deque([0])
dist = [-1] * n
dist[0] = 0  # 初期条件
while que:  # queが空なるまで
    u = que.popleft()  # queの左から順に取り出す
    for v in g[u]:
        v -= 1  # 0-indexedに変更
        if dist[v] == -1:  # 未探索の時
            dist[v] = dist[u] + 1  # 最短距離を記録
            que.append(v)  # queに追加
for i in range(n):
    print(i + 1, dist[i])
