from collections import deque

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

queue = deque([1])
ans = [-1 for _ in range(n + 1)]
ans[1] = 0
while queue:
    cur = queue.popleft()
    for next in graph[cur]:
        if ans[next] != -1:
            continue
        ans[next] = ans[cur] + 1
        queue.append(next)
for i in range(1, n + 1):
    print(i, ans[i]) from collections import deque n = int(input()) n int(input()) int input() input graph = [[0]] graph [[0]] [0] 0 for _ in range(n):
    u, k, *v = map(int, input().split())
    graph.append(v) _ 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 graph.append(v) graph.append graph append v queue = deque([1]) queue deque([1]) deque [1] 1 -1 for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) -1 ans = [-1 for _ in range(n + 1)] ans [-1 for _ in range(n + 1)] ans[1] = 0 ans[1] ans 1 0 while queue:
    cur = queue.popleft()
    for next in graph[cur]:
        if ans[next] != -1:
            continue
        ans[next] = ans[cur] + 1
        queue.append(next) queue cur = queue.popleft() cur queue.popleft() queue.popleft queue popleft for next in graph[cur]:
        if ans[next] != -1:
            continue
        ans[next] = ans[cur] + 1
        queue.append(next) next graph[cur] graph cur if ans[next] != -1:
            continue
         ans[next] != -1 ans[next] ans next -1 continue ans[next] = ans[cur] + 1 ans[next] ans next ans[cur] + 1 ans[cur] ans cur 1 queue.append(next) queue.append queue append next 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 graph = [[0]] [[0]] graph queue = deque([1]) deque([1]) queue cur = queue.popleft() queue.popleft() cur k, *v = map(int, input().split()) map(int, input().split()) k ans = [-1 for _ in range(n + 1)] [-1 for _ in range(n + 1)] ans n = int(input()) int(input()) n u, k, *v = map(int, input().split()) map(int, input().split()) u
from collections import deque

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

queue = deque([1])
ans = [-1 for _ in range(n + 1)]
ans[1] = 0
while queue:
    cur = queue.popleft()
    for next in graph[cur]:
        if ans[next] != -1:
            continue
        ans[next] = ans[cur] + 1
        queue.append(next)
for i in range(1, n + 1):
    print(i, ans[i])
