from collections import deque


INFINITY = 10 ** 10
WHITE = 0
GRAY = 1
BLACK = 2


def breadth_first_search(u_start = 0):
    color[u_start] = GRAY
    d[u_start] = 0
    que.append(u_start)

    while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT
n = int(input())


matrix = [[0] * n for _ in range(n)]

for i in range(n):

    u, k, *V = map(int, input().split())

    for v in V:
        v -= 1
        matrix[i][v] = 1


color = [WHITE] * n
d = [INFINITY] * n
que = deque([])

# PROCESS
breadth_first_search()


# OUTPUT
for i in range(n):
    if d[i] == INFINITY:
        distance = -1
    else:
        distance = d[i]

    print(i + 1, distance)
 from collections import deque INFINITY = 10 ** 10 INFINITY 10 ** 10 10 10 WHITE = 0 WHITE 0 GRAY = 1 GRAY 1 BLACK = 2 BLACK 2 def breadth_first_search(u_start = 0):
    color[u_start] = GRAY
    d[u_start] = 0
    que.append(u_start)

    while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT breadth_first_search color[u_start] = GRAY color[u_start] color u_start GRAY d[u_start] = 0 d[u_start] d u_start 0 que.append(u_start) que.append que append u_start while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT que u = que.popleft() u que.popleft() que.popleft que popleft for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
         v range(n) range n if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
         matrix[u][v] and color[v] == WHITE matrix[u][v] [u] matrix u v color[v] == WHITE color[v] color v WHITE color[v] = GRAY color[v] color v GRAY d[v] = d[u] + 1 d[v] d v d[u] + 1 d[u] d u 1 que.append(v) que.append que append v color[u] = BLACK color[u] color u BLACK u_start = 0 u_start 0 n = int(input()) n int(input()) int input() input [0] * n for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0] * n [0] 0 n matrix = [[0] * n for _ in range(n)] matrix [[0] * n for _ in range(n)] for i in range(n):

    u, k, *V = map(int, input().split())

    for v in V:
        v -= 1
        matrix[i][v] = 1 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:
        v -= 1
        matrix[i][v] = 1 v V v -= 1 v 1 matrix[i][v] = 1 matrix[i][v] [i] matrix i v 1 color = [WHITE] * n color [WHITE] * n [WHITE] WHITE n d = [INFINITY] * n d [INFINITY] * n [INFINITY] INFINITY n que = deque([]) que deque([]) deque [] breadth_first_search() breadth_first_search for i in range(n):
    if d[i] == INFINITY:
        distance = -1
    else:
        distance = d[i]

    print(i + 1, distance) i range(n) range n if d[i] == INFINITY:
        distance = -1
    else:
        distance = d[i]

     d[i] == INFINITY d[i] d i INFINITY distance = -1 distance -1 distance = d[i] distance d[i] d i print(i + 1, distance) print i + 1 i 1 distance d = [INFINITY] * n [INFINITY] * n d matrix = [[0] * n for _ in range(n)] [[0] * n for _ in range(n)] matrix v -= 1 1 v color = [WHITE] * n [WHITE] * n color BLACK = 2 2 BLACK distance = d[i] d[i] distance WHITE = 0 0 WHITE distance = -1 -1 distance def breadth_first_search(u_start = 0):
    color[u_start] = GRAY
    d[u_start] = 0
    que.append(u_start)

    while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT def breadth_first_search(u_start = 0):
    color[u_start] = GRAY
    d[u_start] = 0
    que.append(u_start)

    while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT breadth_first_search INFINITY = 10 ** 10 10 ** 10 INFINITY que = deque([]) deque([]) que n = int(input()) int(input()) n k, *V = map(int, input().split()) map(int, input().split()) k GRAY = 1 1 GRAY u, k, *V = map(int, input().split()) map(int, input().split()) u
from collections import deque


INFINITY = 10 ** 10
WHITE = 0
GRAY = 1
BLACK = 2


def breadth_first_search(u_start = 0):
    color[u_start] = GRAY
    d[u_start] = 0
    que.append(u_start)

    while que:
        u = que.popleft()
        for v in range(n):
            if matrix[u][v] and color[v] == WHITE:
                color[v] = GRAY
                d[v] = d[u] + 1
                que.append(v)
        color[u] = BLACK


# INPUT
n = int(input())


matrix = [[0] * n for _ in range(n)]

for i in range(n):

    u, k, *V = map(int, input().split())

    for v in V:
        v -= 1
        matrix[i][v] = 1


color = [WHITE] * n
d = [INFINITY] * n
que = deque([])

# PROCESS
breadth_first_search()


# OUTPUT
for i in range(n):
    if d[i] == INFINITY:
        distance = -1
    else:
        distance = d[i]

    print(i + 1, distance)

