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

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_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix


def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list


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

    # config length = (vertex_num + 1)
    vertices_visit_queue = deque()
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_distance_list = [-1] * (vertices_num + 1)

    adj_matrix = generate_adj_matrix(vertices_info)
    ans = graph_bfs(v_init=1)
    for j in range(vertices_num):
        print(j + 1, ans[j + 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_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix generate_adj_matrix for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
     v_detail v_info v_adj_list = v_detail[2:] v_adj_list v_detail[2:] v_detail 2: 2 for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
     each v_adj_list init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1 init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] [int(v_detail[0]) - 1] init_adj_matrix int(v_detail[0]) - 1 int(v_detail[0]) int v_detail[0] v_detail 0 1 int(each) - 1 int(each) int each 1 1 return init_adj_matrix init_adj_matrix v_info v_info def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list graph_bfs vertices_status_list[v_init] = VISITED_IN_QUEUE vertices_status_list[v_init] vertices_status_list v_init VISITED_IN_QUEUE vertices_distance_list[v_init] = 0 vertices_distance_list[v_init] vertices_distance_list v_init 0 vertices_visit_queue.appendleft(v_init) vertices_visit_queue.appendleft vertices_visit_queue appendleft v_init while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
     vertices_visit_queue current_vertex = vertices_visit_queue.popleft() current_vertex vertices_visit_queue.popleft() vertices_visit_queue.popleft vertices_visit_queue popleft for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
         v range(1, vertices_num + 1) range 1 vertices_num + 1 vertices_num 1 if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
         adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED adj_matrix[current_vertex - 1][v - 1] [current_vertex - 1] adj_matrix current_vertex - 1 current_vertex 1 v - 1 v 1 vertices_status_list[v] is UNVISITED vertices_status_list[v] vertices_status_list v UNVISITED vertices_status_list[v] = VISITED_IN_QUEUE vertices_status_list[v] vertices_status_list v VISITED_IN_QUEUE vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1 vertices_distance_list[v] vertices_distance_list v vertices_distance_list[current_vertex] + 1 vertices_distance_list[current_vertex] vertices_distance_list current_vertex 1 vertices_visit_queue.append(v) vertices_visit_queue.append vertices_visit_queue append v vertices_status_list[current_vertex] = POPPED_OUT vertices_status_list[current_vertex] vertices_status_list current_vertex POPPED_OUT return vertices_distance_list vertices_distance_list 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_matrix = [[0] * vertices_num for _ in range(vertices_num)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    vertices_visit_queue = deque()
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_distance_list = [-1] * (vertices_num + 1)

    adj_matrix = generate_adj_matrix(vertices_info)
    ans = graph_bfs(v_init=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 [0] * vertices_num for _ in range(vertices_num) for _ in range(vertices_num) _ range(vertices_num) range vertices_num for _ in range(vertices_num) [0] * vertices_num [0] 0 vertices_num init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)] init_adj_matrix [[0] * vertices_num for _ in range(vertices_num)] vertices_visit_queue = deque() vertices_visit_queue deque() deque vertices_status_list = [UNVISITED] * vertices_num vertices_status_list [UNVISITED] * vertices_num [UNVISITED] UNVISITED vertices_num vertices_status_list.insert(0, -1) vertices_status_list.insert vertices_status_list insert 0 -1 vertices_distance_list = [-1] * (vertices_num + 1) vertices_distance_list [-1] * (vertices_num + 1) [-1] -1 vertices_num + 1 vertices_num 1 adj_matrix = generate_adj_matrix(vertices_info) adj_matrix generate_adj_matrix(vertices_info) generate_adj_matrix vertices_info ans = graph_bfs(v_init=1) ans graph_bfs(v_init=1) graph_bfs v_init=1 v_init 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 vertices_status_list = [UNVISITED] * vertices_num [UNVISITED] * vertices_num vertices_status_list _input = sys.stdin.readlines() sys.stdin.readlines() _input vertices_num = int(_input[0]) int(_input[0]) vertices_num def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix generate_adj_matrix vertices_visit_queue = deque() deque() vertices_visit_queue def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list graph_bfs VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 1 VISITED_IN_QUEUE ans = graph_bfs(v_init=1) graph_bfs(v_init=1) ans init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)] [[0] * vertices_num for _ in range(vertices_num)] init_adj_matrix UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 0 UNVISITED adj_matrix = generate_adj_matrix(vertices_info) generate_adj_matrix(vertices_info) adj_matrix vertices_info = list(map(lambda x: x.split(), _input[1:])) list(map(lambda x: x.split(), _input[1:])) vertices_info POPPED_OUT = 0, 1, 2 2 POPPED_OUT vertices_distance_list = [-1] * (vertices_num + 1) [-1] * (vertices_num + 1) vertices_distance_list
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix


def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list


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

    # config length = (vertex_num + 1)
    vertices_visit_queue = deque()
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_distance_list = [-1] * (vertices_num + 1)

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