Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

Python: class Node(object):
    def __init__(self, V):
        self.V = V
        self.d = -1 
  
n = int(input())
nodes = [0] * 101
for i in range(n):
    data = input().split()
    V = list(map(int, data[2:]))
    V.sort(reverse = True)
    nodes[int(data[0])] = Node(V)
  
l = [1,]
l_old = []
cnt = 0

while len(l) > 0:
    l_old = l
    l = []
    for i in l_old:
        if nodes[i].d == -1:
            nodes[i].d = cnt
            V = nodes[i].V
            for j in V:
                if not j in l:
                    if nodes[j].d == -1:
                        l.append(j)
    cnt += 1
for i in range(1, n+1):
    if not isinstance(nodes[i], int):
        print(i, nodes[i].d)

Python: from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

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

colors = [] # 頂点の訪問状態
A = [[0 for j in range(n)] for i in range(n)] # 隣接行列
queue = deque([])

# 隣接行列の初期化
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK

bfs()

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

Python: from collections import deque

n = int(input())
graph = [list(map(int,input().split()))[2:] for _ in range(n)]

dist = [-1]*(n+1)
dist[0] = 0
dist[1] = 0
d = deque()
d.append(1)

while d:
    v = d.popleft()-1
    if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i)

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

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp
# Breadth First Search: python3
#  2018.12.06 yonezawa

import sys
input = sys.stdin.readline
from collections import deque

class node:
    def __init__(self):
        n = self.n = int(input())
        self.cnt = 0
        self.discover = [-1] * n
        self.pos = [1] * n
        self.nmap = [[0 for i in range(n)] for i in range(n)]
    
        for _ in range(n):
            l = list(map(int,input().split()))
            a = l[0] - 1
            for i in l[2:]:
                self.nmap[a][i-1] = 1
    def printMap(self):
        for i in self.nmap:
            print (*i)

    def printResult(self):
        for i in range(self.n):
            print(i+1,self.discover[i])

    def bfs(self,u):
        self.pos[u] = 0
        self.discover[u] = self.cnt
        t = deque()
        t.append(u)
        while t:
            n = t.popleft()
            for i in range(self.n):
                if (self.nmap[n][i] == 1 and self.pos[i] == 1 ):
                    t.append(i)
                    self.pos[i] = 0
                    self.discover[i] = self.discover[n] + 1
                
def main():
    a = node()
    a.bfs(0)
    a.printResult()
    #a.printMap()
if __name__ == '__main__':
    main()

Python: from collections import deque
n = int(input())
G = [[0]*n for _ in range(n)]
for _ in range(n):
    u, k, *V = map(int, input().split())
    if k == 0:
        continue
    else:
        for v in V:
            G[u-1][v-1] = 1



def bfs(u=0):
    Q = deque([])
    color = ['White']*n
    d = [-1]*n

    d[0] = 0
    Q.append(u)
    while Q:
        u = Q.popleft()
        for v in range(n):
            if G[u][v] == 1 and color[v] == 'White':
                if d[v] != -1:
                    continue
                color[v] = 'Gray'
                d[v] = d[u] + 1
                Q.append(v)
        color[u] = 'Black'

    return d

D = bfs()
for i in range(n):
    print('{} {}'.format(i+1, D[i]))

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
frag, T, L,Q = [0]*N, [0]*N, [0]*N, [0]
a,frag[0] = 100, 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(N):
    if L[i] == 0:
        L[i] == -1

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

Python: def BFS(s, r):
    flag[s] = 1
    d[s] = r
    for i in range(1, n+1):
        if G[s][i] == 1 and flag[i] == 0:
            BFS(i, r+1)

n = int(raw_input())
G = [[0 for i in range(n+1)] for j in range(n+1)]
d = [-1 for i in range(n+1)]

for i in range(n):
    v = map(int, raw_input().split())
    for j in range(v[1]):
        G[v[0]][v[2+j]] = 1

flag = [0 for i in range(n+1)]
BFS(1, 0)

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

Python: N = int(input())
G = [None for i in range(N)]
for i in range(N):
    u, k, *vs = map(int, input().split())
    G[u-1] = [e-1 for e in vs]


from collections import deque
dist = [-1]*N
que = deque([0])
dist[0] = 0
while que:
    v = que.popleft()
    d = dist[v]
    for w in G[v]:
        if dist[w] > -1:
            continue
        dist[w] = d + 1
        que.append(w)

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