Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
frag, T, L,Q = [0]*N, [0]*N, [0]*N, [0]
frag[0] = 1
def func(ide):
    for i in E[ide]:
        i = int(i) - 1
        if frag[i] != 1:frag[i] = 1;L[i] = L[ide] +1 ;Q.append(i) 
        else:return

while Q:top = Q[0];func(top);del Q[0]
    
for i in range(1,N):
    if int(L[i]) == 0:
        L[i] = -1

for i in range(N):print(i + 1 ,int(L[i]))

Python: #from collections import deque
import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**6)

#def mypopleft(que, index):
#    if len(que) > index:
#        return que.pop(index)
#    else:
#        return []


def main():
    # input
    n = int(input())

    graph = []
    for i in range(n):
        u, k, *tmp = [int(i) for i in readline().split()]
        graph.append(tmp)
    
    #print(graph)
    d = [-1]*n
    #que = deque([1])
    que = [1]
    #count = 0
    d[0] = 0
    check = []

    while True:
        v = que.pop(0)
        #print(v)
        #count += 1
        #print(d)
        for u in graph[v-1]:
            #print(u)
            if d[u-1] == -1:
                d[u-1] = d[v-1] + 1
                que.append(u)
        if len(que) == 0:
            break
    
    for i in range(n):
        print("{} {}".format(i+1, d[i]))


if __name__ == "__main__":
    main()

Python: from sys import maxsize


def read_adj():
    n = int(input())
    adj_list = [[] for _ in range(n + 1)]
    for _ in range(1, n + 1):
        u, _, *adj = [int(x) for x in input().split()]
        adj_list[u] = adj
    return n, adj_list


def shortest_paths(n, adj_list, ini_key):
    dist = [maxsize] * (n + 1)

    S = [ini_key]
    dist[ini_key] = 0

    while S != []:
        key = S.pop()
        d = dist[key] + 1
        for k in adj_list[key]:
            if d < dist[k]:
                dist[k] = d
                S.append(k)

    return dist


def print_dist(n, dist):
    for i in range(1, n + 1):
        if dist[i] == maxsize:
            print(i, -1)
        else:
            print(i, dist[i])


def main():
    n, adj_list = read_adj()
    dist = shortest_paths(n, adj_list, 1)
    print_dist(n, dist)


if __name__ == '__main__':
    main()

Python: n = int(input())
G = {}
D = [-1] * n
for _ in range(n):
    A = list(map(int, input().split()))
    G[A[0] - 1] = [a - 1 for a in A[2:]]

# print(G)

q = [(0, 0)]
while len(q) > 0:
    v, d = q[0]
    del q[0]
    if D[v] >= 0:
        continue
    D[v] = d

    for nv in G[v]:
        q.append((nv, d+1))

for i in range(n):
    print(i+1, D[i])

Python: from collections import deque

N = int(input())
graph = {i:[] for i in range(N)}

for i in range(N):
    g = list(map(int, input().split()))
    for j in range(g[1]):
        graph[g[0]-1].append(g[j+2]-1)
        
que = deque()
dist = [-1 for _ in range(N)]
que.append(0)
dist[0] = 0

while que:
    top = que.popleft()
    
    for i in graph[top]:
        if(dist[i] != -1):
            continue
        dist[i] = dist[top]+1
        que.append(i)

for i in range(len(dist)):
    print(i+1, dist[i])

Python: n = int(input())
v = []
for i in range(n):
    v_i = list(map(int,input().split()))[2:]
    v.append(v_i)
    
d = [-1]*n
d[0]=0
queue = [1]
dis = 1
while len(queue)!=0:
    queue2 = []
    for i in queue:
        for nex in v[i-1]:
            if d[nex-1]==-1:
                d[nex-1]=dis
                queue2.append(nex)
    dis +=1
    queue = queue2

for i in range(n):
    print(i+1,d[i])