from sys import stdin
from collections import deque
n = int(input())
d = [-1] * (n + 1)
G = [0] + [list(map(int, input().split()[2:])) for _ in range(n)]
d[1] = 0
dq = deque([1])
while len(dq) != 0:
    v = dq.popleft()
    for c in G[v]:
        if d[c] == -1 :
            d[c] = d[v] + 1
            dq.append(c)
for i, x in enumerate(d[1:], start=1):
    print(i, x) from sys import stdin from collections import deque n = int(input()) n int(input()) int input() input d = [-1] * (n + 1) d [-1] * (n + 1) [-1] -1 n + 1 n 1 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:])) list map(int, input().split()[2:]) map int input().split()[2:] ().split() ().split () input split 2: 2 G = [0] + [list(map(int, input().split()[2:])) for _ in range(n)] G [0] + [list(map(int, input().split()[2:])) for _ in range(n)] [0] 0 [list(map(int, input().split()[2:])) for _ in range(n)] d[1] = 0 d[1] d 1 0 dq = deque([1]) dq deque([1]) deque [1] 1 while len(dq) != 0:
    v = dq.popleft()
    for c in G[v]:
        if d[c] == -1 :
            d[c] = d[v] + 1
            dq.append(c) len(dq) != 0 len(dq) len dq 0 v = dq.popleft() v dq.popleft() dq.popleft dq popleft for c in G[v]:
        if d[c] == -1 :
            d[c] = d[v] + 1
            dq.append(c) c G[v] G v if d[c] == -1 :
            d[c] = d[v] + 1
            dq.append(c) d[c] == -1 d[c] d c -1 d[c] = d[v] + 1 d[c] d c d[v] + 1 d[v] d v 1 dq.append(c) dq.append dq append c for i, x in enumerate(d[1:], start=1):
    print(i, x) i x enumerate(d[1:], start=1) enumerate d[1:] d 1: 1 start=1 start 1 print(i, x) print i x n = int(input()) int(input()) n d = [-1] * (n + 1) [-1] * (n + 1) d dq = deque([1]) deque([1]) dq v = dq.popleft() dq.popleft() v G = [0] + [list(map(int, input().split()[2:])) for _ in range(n)] [0] + [list(map(int, input().split()[2:])) for _ in range(n)] G
from sys import stdin
from collections import deque
n = int(input())
d = [-1] * (n + 1)
G = [0] + [list(map(int, input().split()[2:])) for _ in range(n)]
d[1] = 0
dq = deque([1])
while len(dq) != 0:
    v = dq.popleft()
    for c in G[v]:
        if d[c] == -1 :
            d[c] = d[v] + 1
            dq.append(c)
for i, x in enumerate(d[1:], start=1):
    print(i, x)