Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

Python: from collections import deque
from enum import Enum

class Bfs(object):
    """ Breadth First Search """
    class Status(Enum):
        """ ?????????????¨??????¶??? """
        white = 1  # ????¨????
        gray = 2  # ?¨???????
        black = 3  #?¨???????

    def __init__(self, data):
        num_of_nodes = len(data)+1
        self.color = [Bfs.Status.white] * (num_of_nodes + 1)  # ????????????????¨??????¶???
        self.Q = deque()  # ?¨???????????????????????????¨????????\???
        self.M = self._make_matrix(data)  # data????????????????????£??\??????
        self.D = [-1] * (len(data) + 1)  # ????????????????????¢ (-1????????????????????°????????????)

    def _make_matrix(self, data):
        """ data(??£??\?????????)???????????£??\??????????????? (M[i][j]?????¢??§??????????????????+1??????????????????????????????) """
        num = len(data)
        graph_table = [[0] * (num+1) for _ in range(num+1)]
        for d in data:
            node_id = d[0]
            num_of_e = d[1]
            for e in d[2:]:
                graph_table[node_id][e] = 1
        return graph_table

    def search(self, start):
        """ start????????????????????????????????°??????????????? """
        self.Q.append(start)
        self.color[start] = Bfs.Status.gray
        self.D[start] = 0  # ???????????§????????¢???0

        while self.Q:
            u = self.Q.popleft()  # ?????¨?¨????????????????????????°??????
            edges = self.M[u]
            for i in range(1, len(edges)):
                if edges[i] == 1 and self.color[i] == Bfs.Status.white:
                    self.color[i] = Bfs.Status.gray
                    if self.D[i] == -1:
                        self.D[i] = self.D[u] + 1
                        self.Q.append(i)  # ?¨??????????????????????????¬??????????????????? ???????????????????????????self.S[-1]?????????????????????


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = []
    for _ in range(num):
        data.append(list(map(int, input().split(' '))))

    # ???????????????
    # data[]????????????????????????1?????????????????£??\?????????????????????????´?????????????????????¨????????????????????????(??´???????????£??????????????????????????????)
    d = Bfs(data)
    d.search(1)

    # ???????????????
    for i in range(1, num+1):
        print('{0} {1}'.format(i, d.D[i]))

Python: n = int(input())

m = [[0 for _ in range(n)] for _ in range(n)]

for _ in range(n):
    tmp = [int(x) for x in input().split()]
    u = tmp[0]
    k = tmp[1]
    l = tmp[2:]
    for x in l:
        m[u-1][x-1] = 1

dl = [float('inf') for _  in range(n)]
dl[0] = 0

q = [0]

while q:
    t = q.pop()
    tl = m[t]
    for i,x in enumerate(tl):
        if x == 1:
            if dl[i] > dl[t] + 1:
                q.append(i)
                dl[i] = dl[t] + 1
            else:
                pass
        else:
            pass


for i,x in enumerate(dl):
    if x == float('inf'):
        print(i+1, -1)
    else:
        print(i+1, x)

Python: class Myqueue():

    def __init__(self, size):
        self.size = size
        self.N = [None for _ in range(size)]
        self.head = 0
        self.tail = 0

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.size

    def enqueue(self, x):
        if self.is_full():
            raise OverflowError
        self.N[self.tail] = x
        self.tail += 1
        if self.tail == self.size:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        x = self.N[self.head]
        self.head += 1
        if self.head == self.size:
            self.head = 0
        return x


class Node:

    def __init__(self):
        self.distance = -1
        self.status = 0

    def input_data(self, nodes):
        data = input().split()
        self.number = int(data[0])
        self.v = [nodes[int(x) - 1] for x in data[2:]]

n = int(input())
queue = Myqueue(n)
nodes = [Node() for _ in range(n)]
[node.input_data(nodes) for node in nodes]
nodes[0].status = 1
nodes[0].distance = 0
queue.enqueue(nodes[0])
while not queue.is_empty():
    u = queue.dequeue()
    for v in u.v: #type: Node
        if v.status == 0:
            v.status = 1
            v.distance = u.distance + 1
            queue.enqueue(v)
    u.status = 2


nodes.sort(key=lambda i: i.number)
for node in nodes:
    print(f"{node.number} {node.distance}")

Python: from collections import deque

n = int(input())
points = [0 for i in range(n)]

class Point:
    def __init__(self, number, arrow):
        self.number = number        
        self.arrow = arrow
        self.destination = []
        self.distance = -1
        self.hadSearched = False

for i in range(n):
    L = list(map(int, input().split()))
    points[i] = Point(i+1, L[1])
    if L[1] >= 1:
        points[i].destination = L[2:]

def bfs(coord):
    dq = deque()
    coord.hadSearched = True
    dq.appendleft((0, coord))

    while dq:
        steps, po = dq.pop()
        po.distance = steps

        if len(po.destination) > 0:
            for des in po.destination:
                if points[int(des)-1].hadSearched == False:
                    points[int(des)-1].hadSearched = True
                    dq.appendleft((steps+1, points[int(des)-1]))

bfs(points[0])

for i in range(n):
    print("{} {}".format(i+1, points[i].distance))

Python: ii = lambda : int(input())
li = lambda : list(map(int,input().split()))

from collections import deque

class tree:
    def __init__(self,n):
        self.n = n
        self.arr = [[] for i in range(self.n)]
        self.root = [-1]*n

    def add_arr(self, u, v):
        if v[0] != 0:
            self.arr[u-1] = v[1:]

n = ii()
t = tree(n)
for i in range(n):
    uk = li()
    t.add_arr(uk[0],uk[1:])
l = deque()
l.append(0)
t.root[0] = 0
while len(l)>0:
    p = l.popleft()
    for i in t.arr[p]:
        if t.root[i-1] == -1:
            t.root[i-1] = t.root[p]+1
            l.append(i-1)

for k,i in enumerate(t.root):
    print(k+1,i)