Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

Python: # -*- coding: utf-8 -*-

import sys
import os
import math


N = int(input())

E = []
E.append([])
d = [-1] * (N + 1)

for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E)

q = []
q.append(1)

visited = [False] * (N + 1)
d[1] = 0
visited[1] = True

while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

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

Python: 
import queue

WHITE = 0
GRAY = 1
BLACK = 2

NIL = -1
INF = 1000000000


def bfs(u):
    global Q
    Q.put(u)
    for i in range(n):
        d[i] = NIL
    d[u] = 0
    while not Q.empty():
        u = Q.get()
        for v in range(n):
            if (m[u][v] == 1) and (d[v] == NIL):
                d[v] = d[u] + 1
                Q.put(v)
n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]
vid = [0] * n
d = [0] * n
f = [0] * n
Q = queue.Queue()
color = [WHITE] * n
time = 0
nt = [0] * n

tmp = []
for i in range(n):
    nums=list(map(int,input().split()))
    tmp.append(nums)
    vid[i] = nums[0]

for i in range(n):
    for j in range(tmp[i][1]):
        m[i][vid.index(tmp[i][j + 2])] = 1

bfs(0)

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

Python: from collections import deque
N = input()
d = [float('inf')] * N
m = [[0] * N for i in range(N)]
Q = deque([])

def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in range(N):
            if (m[u][v] and  d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v)




if __name__ == '__main__':

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i][j-1] = 1
    
    bfs(0)

    for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i]

Python: from collections import deque

N = 100
n = 0
M = None
WHITE = 0
GRAY = 1
BLACK = 2
INF = float('inf')
d = None


def main():
    global n, M, d
    M = [[0 for _ in range(N)] for _ in range(N)]
    n = int(input())
    d = [INF for _ in range(n)]

    for _ in range(n):
        u, k, *v_s = map(int, input().split(' '))
        for j in range(k):
            M[u-1][v_s[j-1]-1] = 1

    bfs()

    for i in range(n):
        print('{} {}'.format(i+1, d[i] if d[i] != INF else -1))


def bfs():
    q = deque()
    q.appendleft(0)
    d[0] = 0

    while len(q) > 0:
        u = q.pop()
        for v in range(n):
            if M[u][v] == WHITE:
                continue
            if d[v] != INF:
                continue
            d[v] = GRAY
            d[v] = d[u] + 1
            q.appendleft(v)


if __name__ == '__main__':
    main()

Python: # -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

E = []
E.append([])
d = [None] * (N + 1)

for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E)

q = []
q.append(1)

visited = [False] * (N + 1)
d[1] = 0
visited[1] = True

while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

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

Python: import collections
n= int(input())
g=[]
for _ in range(n):
    v,k,*varray= map(int,input().split())
    g.append(varray)
        

d= [-1]* (n+10)
d[0]=0
q= collections.deque()
q.append(0)
while len(q)>  0:
    cur = q.popleft()
    for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
            
for i in range(n):
    print(i+1,d[i])

Python: n=int(input())
d=[0]+[float('inf')]*(n-1)

v=[[]]*n
for i in range(n):
    tmp=list(map(int,input().split()))
    tmp=[i-1 for i in tmp[2:]]
    v[i]=tmp

que=[0]
while que!=[]:
    m=que.pop(0)
    for i in v[m]:
        if d[i]==float('inf'):
            que.append(i)
            d[i]=d[m]+1
for i in range(n):
    if d[i]==float('inf'):
        d[i]=-1
    print(i+1,d[i])

Python: from collections import deque


def bfs(g, source):
    n = len(g)
    d = [float('inf') for _ in range(n)]
    d[0] = 0
    queue = deque()
    queue.append(0)
    while len(queue) > 0:
        cur = queue.popleft()
        for next in g[cur]:
            if d[cur] + 1 < d[next]:
                d[next] = d[cur] + 1
                queue.append(next)
    return d


def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n):
        inp = list(map(int, input().split()))
        m = inp[0] - 1
        k = inp[1]
        for i in range(k):
            g[m].append(inp[i+2] - 1)
    d = bfs(g, 0)
    for i in range(n):
        if d[i] == float('inf'):
            d[i] = -1
        print(i+1, d[i])


if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from collections import deque,defaultdict,Counter
def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t)
if __name__ == '__main__':
    main()