Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

Python: from collections import deque


def bfs(root):
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        for v in n:
            if adjMat[i][v] == 1 and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'
        i = Q.popleft()


n = int(input())
adjMat = [[0] * n for _ in range(n)]
color = ['white'] * n
distance = [-1] * n

for _ in range(n):
    adj = list(map(int, input().split()))
    i = adj[0]
    v = adj[2:]
    for j in v:
        adjMat[i - 1][j - 1] = 1

bfs(0)

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

Python: n = int(raw_input()) 
G = [0 for i in range(n)] 
a = [[0 for i in range(n)] for j in range(n)]
dis = [10000 for i in range(n)]
dis[0] = 0

for i in range(n):
    G = map(int, raw_input().split())
    for j in range(G[1]):
        a[G[0]-1][G[2+j]-1] = 1
 
def bfs_visit(s):
    for k in range(n):
      if a[s][k] == 1:
        if dis[k] > dis[s] + 1:
          dis[k] = dis[s] + 1
          bfs_visit(k)

for s in range(n):
    bfs_visit(s)
for s in range(n):
    if dis[s] == 10000:
       dis[s] = -1
for s in range(n):
    print (s+1, d[s])

Python: from collections import deque

# データの入力
N = int(input())
DG = {}
for i in range(N):
    tmp = input().split()
    if int(tmp[1]) != 0:
        DG[i] = [int(x)-1 for x in tmp[2:]]
    else:
        DG[i] = []


result = {}
is_visited = {key: False for key in range(N)}
# bfs
# 初期queue
que = deque([(0, 0)])  # 必要なものはnodeidと原点からの距離
while que:
    nodeid, cost = que.popleft()  # 先入れ先出し
    result[nodeid] = cost
    is_visited[nodeid] = True  # この文は最初の1for目しか意味ないんだけどなんかうまい方法ないのか
    for nextnode in DG[nodeid]:
        if is_visited[nextnode]:
            pass
        else:  # 未訪問のものについては探索候補に入れる
            que.append((nextnode, cost + 1))
            is_visited[nextnode] = True  # 探索候補に入れた瞬間に訪問管理しないと重複を生じる場合がある

for i in range(N):
    if is_visited[i]:
        print(i + 1, result[i])
    else:
        print(i+1, -1)

Python: n = int(raw_input())
  
G = [0 for i in range(n)] 
a = [[0 for i in range(n)] for j in range(n)]
dis = [-1 for i in range(n)]
dis[0] = 0
 
for i in range(n):
    G = map(int, raw_input().split())
    for j in range(G[1]):
        a[G[0]-1][G[2+j]-1] = 1
  
def bfs_visit(s):
    for k in range(n):
      if a[s][k] == 1:
       if dis[k] == -1:
         dis[k] = dis[s] + 1
         bfs_visit(k)
 
for s in range(n):
    bfs_visit(s)
for s in range(n):
    print (s+1,dis[s])

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:
        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
n = int(input())
M = [[0]*n for _ in range(n)]
d = [-1]*n
st = [0]*n
for _ in range(n):
    i = [int(i) for i in input().split()]
    u = i[0]
    k = i[1]
    V = i[2:]
    for v in V: M[u-1][v-1] = 1

def bfs(s):
    Q = deque()
    Q.append(s)
    st[s] = 1
    d[s] = 0
    while Q:
        u = Q.popleft()
        for v in range(n):
            if M[u][v] and st[v] == 0:
                st[v] = 1
                Q.append(v)
                d[v] = d[u]+1
        st[u] = 2
    return

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

Python: from collections import deque

n = int(input())

adj = [0]*n # adjacent list
d = [-1]*n # distance from the source vertex
c = ['w']*n # the 'color' of each vertex

# receiving input (the structure of the graph)
for i in range(n):
    tmp = list(map( int, input().split() ) )
    adj[tmp[0]-1] = list(map(lambda x : x - 1, tmp[2:]))

# set the source vertex
s = 0
d[s] = 0
c[s] = 'g'


Q = deque()
Q.append(s)

while Q:
    u = Q.popleft()
    for v in adj[u]:
        if c[v] == 'w':
            c[v] = 'g'
            d[v] = d[u] + 1
            Q.append(v)
    c[u] = 'b'
    

for i in range(n):
    print(f'{i+1} {d[i]}')

Python: from sys import stdin
from collections import deque
n = int(stdin.readline())
d = [-1] * (n + 1)
def bfs(G):
    d[1] = 0
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x)
G = [0]
for i in range(n):
    G.append(list(map(int, stdin.readline().split()[2:])))
bfs(G)

Python: def BFS(G):
    ans = [0] + [[i+1,-1] for i in range(len(G))]
    def main(G,i,d):
        ans[i] = [i, d]
        for j in G[i-1][2:]: main(G,j,d+1)
    main(G,1,0)
    return ans[1:]


if __name__=='__main__':
    n = int(input())
    G = [list(map(int,input().split())) for _ in range(n)]
    for out in BFS(G): print(*out)

Python: n = int(input())
G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)]
# print(G)
from collections import deque
d = deque([1])
dist  = [10**10 for _ in range(n+1)]
dist[1] = 0
while len(d) != 0:
    now = d.popleft()
    # print(now)
    for nexte in G[now]:
        if dist[now] +1 < dist[nexte]:
            dist[nexte] = dist[now] + 1
            d.append(nexte)
for i,d in enumerate(dist[1:]):
    if d != 10**10:
        print(i+1,d)
    else:
        print(i+1,-1)