#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
4
1 2 2 4
2 1 4
3 0
4 1 3

output:
1 0
2 1
3 2
4 1
"""

import sys
from collections import deque

UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
    return init_adj_table


def graph_bfs(v_init):
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
    return distance


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_table = [[] for _ in range(vertices_num + 1)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    queue = deque()
    status = [UNVISITED] * (vertices_num + 1)
    distance = [-1] * (vertices_num + 1)

    adj_table = generate_adj_matrix(vertices_info)
    ans = graph_bfs(1)
    for j in range(vertices_num):
        print(j + 1, ans[j + 1]) """
input:
4
1 2 2 4
2 1 4
3 0
4 1 3

output:
1 0
2 1
3 2
4 1
""" import sys sys from collections import deque UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 UNVISITED 0 VISITED_IN_QUEUE 1 POPPED_OUT 2 def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
    return init_adj_table generate_adj_matrix for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
     v_detail v_info v_index, adj_num, *adj_list = map(int, v_detail) v_index map(int, v_detail) map int v_detail adj_num *adj_list adj_list init_adj_table[v_index].extend(adj_list) [v_index].extend [v_index] init_adj_table v_index extend adj_list return init_adj_table init_adj_table v_info v_info def graph_bfs(v_init):
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
    return distance graph_bfs status[v_init] = VISITED_IN_QUEUE status[v_init] status v_init VISITED_IN_QUEUE distance[v_init] = 0 distance[v_init] distance v_init 0 queue.appendleft(v_init) queue.appendleft queue appendleft v_init while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
     queue current = queue.popleft() current queue.popleft() queue.popleft queue popleft for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
         v adj_table[current] adj_table current if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
         status[v] is UNVISITED status[v] status v UNVISITED status[v] = VISITED_IN_QUEUE status[v] status v VISITED_IN_QUEUE distance[v] = distance[current] + 1 distance[v] distance v distance[current] + 1 distance[current] distance current 1 queue.append(v) queue.append queue append v status[current] = POPPED_OUT status[current] status current POPPED_OUT return distance distance v_init v_init if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_table = [[] for _ in range(vertices_num + 1)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    queue = deque()
    status = [UNVISITED] * (vertices_num + 1)
    distance = [-1] * (vertices_num + 1)

    adj_table = generate_adj_matrix(vertices_info)
    ans = graph_bfs(1)
    for j in range(vertices_num):
        print(j + 1, ans[j + 1]) __name__ == '__main__' __name__ '__main__' _input = sys.stdin.readlines() _input sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines vertices_num = int(_input[0]) vertices_num int(_input[0]) int _input[0] _input 0 vertices_info = list(map(lambda x: x.split(), _input[1:])) vertices_info list(map(lambda x: x.split(), _input[1:])) list map(lambda x: x.split(), _input[1:]) map lambda x: x.split() x.split() x.split x split x _input[1:] _input 1: 1 [] for _ in range(vertices_num + 1) for _ in range(vertices_num + 1) _ range(vertices_num + 1) range vertices_num + 1 vertices_num 1 for _ in range(vertices_num + 1) [] init_adj_table = [[] for _ in range(vertices_num + 1)] init_adj_table [[] for _ in range(vertices_num + 1)] queue = deque() queue deque() deque status = [UNVISITED] * (vertices_num + 1) status [UNVISITED] * (vertices_num + 1) [UNVISITED] UNVISITED vertices_num + 1 vertices_num 1 distance = [-1] * (vertices_num + 1) distance [-1] * (vertices_num + 1) [-1] -1 vertices_num + 1 vertices_num 1 adj_table = generate_adj_matrix(vertices_info) adj_table generate_adj_matrix(vertices_info) generate_adj_matrix vertices_info ans = graph_bfs(1) ans graph_bfs(1) graph_bfs 1 for j in range(vertices_num):
        print(j + 1, ans[j + 1]) j range(vertices_num) range vertices_num print(j + 1, ans[j + 1]) print j + 1 j 1 ans[j + 1] ans j + 1 j 1 distance = [-1] * (vertices_num + 1) [-1] * (vertices_num + 1) distance vertices_info = list(map(lambda x: x.split(), _input[1:])) list(map(lambda x: x.split(), _input[1:])) vertices_info UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 0 UNVISITED queue = deque() deque() queue def graph_bfs(v_init):
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
    return distance def graph_bfs(v_init):
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
    return distance graph_bfs ans = graph_bfs(1) graph_bfs(1) ans vertices_num = int(_input[0]) int(_input[0]) vertices_num status = [UNVISITED] * (vertices_num + 1) [UNVISITED] * (vertices_num + 1) status adj_table = generate_adj_matrix(vertices_info) generate_adj_matrix(vertices_info) adj_table def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
    return init_adj_table def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
    return init_adj_table generate_adj_matrix _input = sys.stdin.readlines() sys.stdin.readlines() _input VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 1 VISITED_IN_QUEUE POPPED_OUT = 0, 1, 2 2 POPPED_OUT init_adj_table = [[] for _ in range(vertices_num + 1)] [[] for _ in range(vertices_num + 1)] init_adj_table
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
4
1 2 2 4
2 1 4
3 0
4 1 3

output:
1 0
2 1
3 2
4 1
"""

import sys
from collections import deque

UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_index, adj_num, *adj_list = map(int, v_detail)
        # assert len(adj_list) == adj_num
        init_adj_table[v_index].extend(adj_list)
    return init_adj_table


def graph_bfs(v_init):
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for v in adj_table[current]:
            if status[v] is UNVISITED:
                status[v] = VISITED_IN_QUEUE
                distance[v] = distance[current] + 1
                queue.append(v)
        status[current] = POPPED_OUT
    return distance


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_table = [[] for _ in range(vertices_num + 1)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    queue = deque()
    status = [UNVISITED] * (vertices_num + 1)
    distance = [-1] * (vertices_num + 1)

    adj_table = generate_adj_matrix(vertices_info)
    ans = graph_bfs(1)
    for j in range(vertices_num):
        print(j + 1, ans[j + 1])