from collections import deque
from collections import defaultdict

n = int(input())

graph = defaultdict(list)

for i in range(n):
    u, k, *v = map(int, input().split())
    for v in v:
        graph[u].append(v)

ans = [-1] * (n + 1)

q = deque([1])
ans[1] = 0

while q:
    c = q.popleft()
    for v in graph[c]:
        if ans[v] == -1:
            ans[v] = ans[c] + 1
            q.append(v)

for i in range(1, n + 1):
    print(i, ans[i]) from collections import deque from collections import defaultdict n = int(input()) n int(input()) int input() input graph = defaultdict(list) graph defaultdict(list) defaultdict list for i in range(n):
    u, k, *v = map(int, input().split())
    for v in v:
        graph[u].append(v) i range(n) range n u, k, *v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split k *v v for v in v:
        graph[u].append(v) v v graph[u].append(v) [u].append [u] graph u append v ans = [-1] * (n + 1) ans [-1] * (n + 1) [-1] -1 n + 1 n 1 q = deque([1]) q deque([1]) deque [1] 1 ans[1] = 0 ans[1] ans 1 0 while q:
    c = q.popleft()
    for v in graph[c]:
        if ans[v] == -1:
            ans[v] = ans[c] + 1
            q.append(v) q c = q.popleft() c q.popleft() q.popleft q popleft for v in graph[c]:
        if ans[v] == -1:
            ans[v] = ans[c] + 1
            q.append(v) v graph[c] graph c if ans[v] == -1:
            ans[v] = ans[c] + 1
            q.append(v) ans[v] == -1 ans[v] ans v -1 ans[v] = ans[c] + 1 ans[v] ans v ans[c] + 1 ans[c] ans c 1 q.append(v) q.append q append v for i in range(1, n + 1):
    print(i, ans[i]) i range(1, n + 1) range 1 n + 1 n 1 print(i, ans[i]) print i ans[i] ans i q = deque([1]) deque([1]) q c = q.popleft() q.popleft() c k, *v = map(int, input().split()) map(int, input().split()) k u, k, *v = map(int, input().split()) map(int, input().split()) u graph = defaultdict(list) defaultdict(list) graph n = int(input()) int(input()) n ans = [-1] * (n + 1) [-1] * (n + 1) ans
from collections import deque
from collections import defaultdict

n = int(input())

graph = defaultdict(list)

for i in range(n):
    u, k, *v = map(int, input().split())
    for v in v:
        graph[u].append(v)

ans = [-1] * (n + 1)

q = deque([1])
ans[1] = 0

while q:
    c = q.popleft()
    for v in graph[c]:
        if ans[v] == -1:
            ans[v] = ans[c] + 1
            q.append(v)

for i in range(1, n + 1):
    print(i, ans[i])
