from collections import deque

n = int(input())
graph = []

for _ in range(n):
    u = list(map(int, input().split()))

    if u[1] == 0:
        graph.append([])
    else:
        graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2])))

dist = [-1] * n


def bfs(x):
    q = deque([])
    dist[x] = 0
    q.append(x)

    while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx)


bfs(0)

for i in range(n):
    print(i + 1, dist[i])
 from collections import deque n = int(input()) n int(input()) int input() input graph = [] graph [] for _ in range(n):
    u = list(map(int, input().split()))

    if u[1] == 0:
        graph.append([])
    else:
        graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2]))) _ range(n) range n u = list(map(int, input().split())) u list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if u[1] == 0:
        graph.append([])
    else:
        graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2]))) u[1] == 0 u[1] u 1 0 graph.append([]) graph.append graph append [] graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2]))) graph.append graph append list(map(lambda x: x - 1, u[2: u[1] + 2])) list map(lambda x: x - 1, u[2: u[1] + 2]) map lambda x: x - 1 x - 1 x 1 x u[2: u[1] + 2] u 2: u[1] + 2 2 u[1] + 2 u[1] u 1 2 dist = [-1] * n dist [-1] * n [-1] -1 n def bfs(x):
    q = deque([])
    dist[x] = 0
    q.append(x)

    while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) bfs q = deque([]) q deque([]) deque [] dist[x] = 0 dist[x] dist x 0 q.append(x) q.append q append x while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) q x = q.popleft() x q.popleft() q.popleft q popleft for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) nx graph[x] graph x if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) dist[nx] != -1 dist[nx] dist nx -1 continue dist[nx] = dist[x] + 1 dist[nx] dist nx dist[x] + 1 dist[x] dist x 1 q.append(nx) q.append q append nx x x bfs(0) bfs 0 for i in range(n):
    print(i + 1, dist[i]) i range(n) range n print(i + 1, dist[i]) print i + 1 i 1 dist[i] dist i def bfs(x):
    q = deque([])
    dist[x] = 0
    q.append(x)

    while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) def bfs(x):
    q = deque([])
    dist[x] = 0
    q.append(x)

    while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx) bfs u = list(map(int, input().split())) list(map(int, input().split())) u graph = [] [] graph n = int(input()) int(input()) n dist = [-1] * n [-1] * n dist
from collections import deque

n = int(input())
graph = []

for _ in range(n):
    u = list(map(int, input().split()))

    if u[1] == 0:
        graph.append([])
    else:
        graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2])))

dist = [-1] * n


def bfs(x):
    q = deque([])
    dist[x] = 0
    q.append(x)

    while q:
        x = q.popleft()

        for nx in graph[x]:
            if dist[nx] != -1:
                continue
            else:
                dist[nx] = dist[x] + 1
                q.append(nx)


bfs(0)

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

