from collections import deque

n = int(input())
graph = [[] for _ in range(n)]
for i in range(n):
    graph[i] += list(map(lambda x: x - 1, list(map(int, input().split()))[2:]))

visited = [-1] * n
que = deque()
# 頂点0を入れておく
que.append(0)
visited[0] = 0
cnt = 0

while que:
    v = que.popleft()
    for nv in graph[v]:
        if not visited[nv] == -1:
            continue
        visited[nv] = visited[v] + 1
        que.append(nv)

for v, d in enumerate(visited, 1):
    print(v, d) from collections import deque n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] graph = [[] for _ in range(n)] graph [[] for _ in range(n)] for i in range(n):
    graph[i] += list(map(lambda x: x - 1, list(map(int, input().split()))[2:])) i range(n) range n graph[i] += list(map(lambda x: x - 1, list(map(int, input().split()))[2:])) graph[i] graph i list(map(lambda x: x - 1, list(map(int, input().split()))[2:])) list map(lambda x: x - 1, list(map(int, input().split()))[2:]) map lambda x: x - 1 x - 1 x 1 x list(map(int, input().split()))[2:] (map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 2: 2 visited = [-1] * n visited [-1] * n [-1] -1 n que = deque() que deque() deque que.append(0) que.append que append 0 visited[0] = 0 visited[0] visited 0 0 cnt = 0 cnt 0 while que:
    v = que.popleft()
    for nv in graph[v]:
        if not visited[nv] == -1:
            continue
        visited[nv] = visited[v] + 1
        que.append(nv) que v = que.popleft() v que.popleft() que.popleft que popleft for nv in graph[v]:
        if not visited[nv] == -1:
            continue
        visited[nv] = visited[v] + 1
        que.append(nv) nv graph[v] graph v if not visited[nv] == -1:
            continue
         not visited[nv] == -1 visited[nv] == -1 visited[nv] visited nv -1 continue visited[nv] = visited[v] + 1 visited[nv] visited nv visited[v] + 1 visited[v] visited v 1 que.append(nv) que.append que append nv for v, d in enumerate(visited, 1):
    print(v, d) v d enumerate(visited, 1) enumerate visited 1 print(v, d) print v d que = deque() deque() que cnt = 0 0 cnt v = que.popleft() que.popleft() v graph = [[] for _ in range(n)] [[] for _ in range(n)] graph visited = [-1] * n [-1] * n visited n = int(input()) int(input()) n
from collections import deque

n = int(input())
graph = [[] for _ in range(n)]
for i in range(n):
    graph[i] += list(map(lambda x: x - 1, list(map(int, input().split()))[2:]))

visited = [-1] * n
que = deque()
# 頂点0を入れておく
que.append(0)
visited[0] = 0
cnt = 0

while que:
    v = que.popleft()
    for nv in graph[v]:
        if not visited[nv] == -1:
            continue
        visited[nv] = visited[v] + 1
        que.append(nv)

for v, d in enumerate(visited, 1):
    print(v, d)
