# ALDS_11_C - 幅優先探索
import sys
import heapq

N = int(input())
G = []
for _ in range(N):
    u, k, *v = map(int, sys.stdin.readline().strip().split())
    v = [i - 1 for i in v]
    G.append(v)


distance = [-1] * N
q = []
fp = [True] * N  # 探索可能か記録する
heapq.heapify(q)
heapq.heappush(q, (0, 0))
fp[0] = False


while q:
    dist, u = heapq.heappop(q)
    distance[u] = dist
    for v in G[u]:
        if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False

for i, d in enumerate(distance):
    print(i + 1, d)
 import sys sys import heapq heapq N = int(input()) N int(input()) int input() input G = [] G [] for _ in range(N):
    u, k, *v = map(int, sys.stdin.readline().strip().split())
    v = [i - 1 for i in v]
    G.append(v) _ range(N) range N u, k, *v = map(int, sys.stdin.readline().strip().split()) u map(int, sys.stdin.readline().strip().split()) map int sys.stdin.readline().strip().split() sys.stdin.readline().strip().split sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip split k *v v i - 1 for i in v for i in v i v for i in v i - 1 i 1 v = [i - 1 for i in v] v [i - 1 for i in v] G.append(v) G.append G append v distance = [-1] * N distance [-1] * N [-1] -1 N q = [] q [] fp = [True] * N fp [True] * N [True] True N heapq.heapify(q) heapq.heapify heapq heapify q heapq.heappush(q, (0, 0)) heapq.heappush heapq heappush q (0, 0) 0 0 fp[0] = False fp[0] fp 0 False while q:
    dist, u = heapq.heappop(q)
    distance[u] = dist
    for v in G[u]:
        if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False q dist, u = heapq.heappop(q) dist heapq.heappop(q) heapq.heappop heapq heappop q u distance[u] = dist distance[u] distance u dist for v in G[u]:
        if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False v G[u] G u if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False fp[v] is True fp[v] fp v True heapq.heappush(q, (dist + 1, v)) heapq.heappush heapq heappush q (dist + 1, v) dist + 1 dist 1 v fp[v] = False fp[v] fp v False for i, d in enumerate(distance):
    print(i + 1, d) i d enumerate(distance) enumerate distance print(i + 1, d) print i + 1 i 1 d u, k, *v = map(int, sys.stdin.readline().strip().split()) map(int, sys.stdin.readline().strip().split()) u N = int(input()) int(input()) N distance = [-1] * N [-1] * N distance u = heapq.heappop(q) heapq.heappop(q) u k, *v = map(int, sys.stdin.readline().strip().split()) map(int, sys.stdin.readline().strip().split()) k dist, u = heapq.heappop(q) heapq.heappop(q) dist fp = [True] * N [True] * N fp q = [] [] q G = [] [] G v = [i - 1 for i in v] [i - 1 for i in v] v
# ALDS_11_C - 幅優先探索
import sys
import heapq

N = int(input())
G = []
for _ in range(N):
    u, k, *v = map(int, sys.stdin.readline().strip().split())
    v = [i - 1 for i in v]
    G.append(v)


distance = [-1] * N
q = []
fp = [True] * N  # 探索可能か記録する
heapq.heapify(q)
heapq.heappush(q, (0, 0))
fp[0] = False


while q:
    dist, u = heapq.heappop(q)
    distance[u] = dist
    for v in G[u]:
        if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False

for i, d in enumerate(distance):
    print(i + 1, d)

