#!/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 graph_bfs(v_init):
    queue = deque()
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

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


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = list(map(lambda x: x.split(), _input[1:]))
    # assert len(vertex_info) == vertex_num

    status = [UNVISITED] * (v_num + 1)
    distance = [-1] * (v_num + 1)

    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(adj_list)

    graph_bfs(v_init=1)
    for index, v in enumerate(distance[1:], 1):
        print(index, v) """
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 graph_bfs(v_init):
    queue = deque()
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

    while queue:
        current = queue.popleft()
        for adj in adj_table[current]:
            if status[adj] is UNVISITED:
                status[adj] = VISITED_IN_QUEUE
                distance[adj] = distance[current] + 1
                queue.append(adj)
        status[current] = POPPED_OUT
    return None graph_bfs queue = deque() queue deque() deque 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 adj in adj_table[current]:
            if status[adj] is UNVISITED:
                status[adj] = VISITED_IN_QUEUE
                distance[adj] = distance[current] + 1
                queue.append(adj)
        status[current] = POPPED_OUT
     queue current = queue.popleft() current queue.popleft() queue.popleft queue popleft for adj in adj_table[current]:
            if status[adj] is UNVISITED:
                status[adj] = VISITED_IN_QUEUE
                distance[adj] = distance[current] + 1
                queue.append(adj)
         adj adj_table[current] adj_table current if status[adj] is UNVISITED:
                status[adj] = VISITED_IN_QUEUE
                distance[adj] = distance[current] + 1
                queue.append(adj)
         status[adj] is UNVISITED status[adj] status adj UNVISITED status[adj] = VISITED_IN_QUEUE status[adj] status adj VISITED_IN_QUEUE distance[adj] = distance[current] + 1 distance[adj] distance adj distance[current] + 1 distance[current] distance current 1 queue.append(adj) queue.append queue append adj status[current] = POPPED_OUT status[current] status current POPPED_OUT return None None v_init v_init if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = list(map(lambda x: x.split(), _input[1:]))
    # assert len(vertex_info) == vertex_num

    status = [UNVISITED] * (v_num + 1)
    distance = [-1] * (v_num + 1)

    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(adj_list)

    graph_bfs(v_init=1)
    for index, v in enumerate(distance[1:], 1):
        print(index, v) __name__ == '__main__' __name__ '__main__' _input = sys.stdin.readlines() _input sys.stdin.readlines() sys.stdin.readlines sys.stdin sys stdin readlines v_num = int(_input[0]) v_num int(_input[0]) int _input[0] _input 0 vertices = list(map(lambda x: x.split(), _input[1:])) vertices 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 status = [UNVISITED] * (v_num + 1) status [UNVISITED] * (v_num + 1) [UNVISITED] UNVISITED v_num + 1 v_num 1 distance = [-1] * (v_num + 1) distance [-1] * (v_num + 1) [-1] -1 v_num + 1 v_num 1 adj_table = tuple([] for _ in range(v_num + 1)) adj_table tuple([] for _ in range(v_num + 1)) tuple [] for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(adj_list)

     v_info vertices v_index, adj_num, *adj_list = map(int, v_info) v_index map(int, v_info) map int v_info adj_num *adj_list adj_list adj_table[v_index].extend(adj_list) [v_index].extend [v_index] adj_table v_index extend adj_list graph_bfs(v_init=1) graph_bfs v_init=1 v_init 1 for index, v in enumerate(distance[1:], 1):
        print(index, v) index v enumerate(distance[1:], 1) enumerate distance[1:] distance 1: 1 1 print(index, v) print index v UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 0 UNVISITED vertices = list(map(lambda x: x.split(), _input[1:])) list(map(lambda x: x.split(), _input[1:])) vertices adj_table = tuple([] for _ in range(v_num + 1)) tuple([] for _ in range(v_num + 1)) adj_table distance = [-1] * (v_num + 1) [-1] * (v_num + 1) distance VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2 1 VISITED_IN_QUEUE def graph_bfs(v_init):
    queue = deque()
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

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

    while queue:
        current = queue.popleft()
        for adj in adj_table[current]:
            if status[adj] is UNVISITED:
                status[adj] = VISITED_IN_QUEUE
                distance[adj] = distance[current] + 1
                queue.append(adj)
        status[current] = POPPED_OUT
    return None graph_bfs status = [UNVISITED] * (v_num + 1) [UNVISITED] * (v_num + 1) status v_num = int(_input[0]) int(_input[0]) v_num v_index, adj_num, *adj_list = map(int, v_info) map(int, v_info) v_index _input = sys.stdin.readlines() sys.stdin.readlines() _input adj_num, *adj_list = map(int, v_info) map(int, v_info) adj_num POPPED_OUT = 0, 1, 2 2 POPPED_OUT
#!/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 graph_bfs(v_init):
    queue = deque()
    status[v_init] = VISITED_IN_QUEUE
    distance[v_init] = 0
    queue.appendleft(v_init)

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


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num = int(_input[0])
    vertices = list(map(lambda x: x.split(), _input[1:]))
    # assert len(vertex_info) == vertex_num

    status = [UNVISITED] * (v_num + 1)
    distance = [-1] * (v_num + 1)

    adj_table = tuple([] for _ in range(v_num + 1))
    for v_info in vertices:
        v_index, adj_num, *adj_list = map(int, v_info)
        # assert len(adj_list) == adj_num
        adj_table[v_index].extend(adj_list)

    graph_bfs(v_init=1)
    for index, v in enumerate(distance[1:], 1):
        print(index, v)