from collections import deque
N = int(input())
graph = [list(map(int, input().split())) for _ in range(N)]
seen = [-1]*(N+1)

todo = deque()


def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i)


todo.append(1)
seen[1] = 0

while len(todo) > 0:
    bfs(todo.popleft())

for i in range(1, N+1):
    print(i, seen[i]) from collections import deque N = int(input()) N int(input()) int input() input list(map(int, input().split())) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split graph = [list(map(int, input().split())) for _ in range(N)] graph [list(map(int, input().split())) for _ in range(N)] seen = [-1]*(N+1) seen [-1]*(N+1) [-1] -1 N+1 N 1 todo = deque() todo deque() deque def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i) bfs u = v-1 u v-1 v 1 next_v = graph[u][2:] next_v graph[u][2:] [u] graph u 2: 2 for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i) i next_v if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i) seen[i] == -1 seen[i] seen i -1 seen[i] = seen[v] + 1 seen[i] seen i seen[v] + 1 seen[v] seen v 1 todo.append(i) todo.append todo append i v v todo.append(1) todo.append todo append 1 seen[1] = 0 seen[1] seen 1 0 while len(todo) > 0:
    bfs(todo.popleft()) len(todo) > 0 len(todo) len todo 0 bfs(todo.popleft()) bfs todo.popleft() todo.popleft todo popleft for i in range(1, N+1):
    print(i, seen[i]) i range(1, N+1) range 1 N+1 N 1 print(i, seen[i]) print i seen[i] seen i N = int(input()) int(input()) N seen = [-1]*(N+1) [-1]*(N+1) seen def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i) def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i) bfs todo = deque() deque() todo graph = [list(map(int, input().split())) for _ in range(N)] [list(map(int, input().split())) for _ in range(N)] graph
from collections import deque
N = int(input())
graph = [list(map(int, input().split())) for _ in range(N)]
seen = [-1]*(N+1)

todo = deque()


def bfs(v):
    u = v-1
    next_v = graph[u][2:]
    for i in next_v:
        if seen[i] == -1:
            seen[i] = seen[v] + 1
            todo.append(i)


todo.append(1)
seen[1] = 0

while len(todo) > 0:
    bfs(todo.popleft())

for i in range(1, N+1):
    print(i, seen[i])
