Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
Q = [0]
L = [0] + [-1]*N
frag = [1] + [0]*N
def func(ide):
    global Q
    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):print(i + 1 ,int(L[i]))

Python: from queue import Queue

n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
d = [-1] * n

for i in range(n):
    u, k, *v = list(map(int, input().split()))
    for j in v:
        A[int(u)-1][int(j)-1] = 1

q = Queue()
q.put(0)
d[0] = 0

while not q.empty():

    u = q.get()

    for v in range(n):

        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

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

Python: from collections import deque

n = int(input())
adj = [list(map(int, input().split(' '))) for i in range(n)]

class Node:
  def __init__(self, id, ck):
    self.id = id
    self.ck = ck
    self.depth = -1

  def __str__(self):
    return '{} {}'.format(self.id, self.depth)

class Graph:
  def __init__(self):
    self.map = {}

  def get(self, id):
    return self.map[id]

  def set(self, id, ck):
    self.map[id] = Node(id, ck)

  def bfs(self):
    root = self.get(1)
    root.depth = 0
    queue = deque()
    queue.append(root)
    while(len(queue) > 0):
      node = queue.popleft()
      for id in node.ck:
        target = self.get(id)
        if target.depth != -1: continue
        target.depth = node.depth + 1
        queue.append(target)

  def __str__(self):
    return '\n'.join(map(str, [self.get(i + 1) for i in range(len(self.map))]))

graph = Graph()
for data in adj:
  id = data[0]
  ck = data[2:]
  graph.set(id, ck)

graph.bfs()
print(graph)

Python: n = int(input())
Adj = [[0 for i in range(n)] for i in range(n)]
 
for i in range(n):
    u = list(map(int, input().split()))
    if u[1] > 0:
        for i in u[2: 2 + u[1]]:
            Adj[u[0] - 1][i - 1] = 1
 
color = [0] * n
d = [-1] * n
import queue
Q = queue.Queue()

color[0] = 1
d[0] = 0
Q.put(1)
while not Q.empty():
    u = Q.get()
    for v in range(1, n+1):
        if Adj[u-1][v-1] == 1 and color[v-1] == 0:
            color[v-1] = 1
            d[v-1] = d[u-1] + 1
            Q.put(v)
    color[u-1] = 2

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

Python: n = int(input())

adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

isSearched = [None] + [False] * n

distance = [None] + [-1] * n

def bfs(u):
    d = 0
    isSearched[u] = True
    distance[u] = d
    edge = [u]
    while edge:
        q = list(edge)
        edge = []
        d += 1
        for c_e in q:
            for n_e in adj[c_e]:
                if not isSearched[n_e]:
                    isSearched[n_e] = True
                    edge.append(n_e)
                    distance[n_e] = d


bfs(1)

for i, x in enumerate(distance[1:], start=1):
    print(i, x)

Python: #Breadth First Search
n=int(input())
V=[]
a=[1]
for i in range(n):
    V.append([int(i) for i in input().split()])
    a.append(0)
V=sorted(V,key=lambda x:x[0])
print('1 0')
d=1
c=0
w=[0]
p=[]
while c<n and w!=[]:
    i=0
    y=[]
 #   print(c)
    while c<n and i<len(w):
        j=2
        while j<2+V[w[i]][1] and c<n:
            if a[V[w[i]][j]-1]==0:
        #        print(a)
                a[V[w[i]][j]-1]=1
                p.append([V[w[i]][j],d])
                c+=1
                y.append(V[w[i]][j]-1)
            j+=1
        i+=1
    w=y
    d+=1
p=sorted(p, key=lambda x:x[0])
#print(p)
i=2
j=0
while i <=n and j<len(p):
    while p[j][0]>i:
        print(str(i) +" -1")
        i+=1
    print(str(p[j][0])+" "+str(p[j][1]))
    i+=1
    j+=1
while i<=n:
    print(str(i)+" -1")
    i+=1

Python: def bfs(G, s):
    d = {key: -1 for key in G.keys()}
    d[s] = 0
    Q = [s]
    while len(Q) > 0:
        u = Q.pop(0)
        for v in G[u]:
            if d[v] < 0:
                d[v] = d[u] + 1
                Q.append(v)

    return d


if __name__ == '__main__':
    G = {}
    n = int(input())
    for _ in range(n):
        tmp = list(map(int, input().split()))
        u = tmp.pop(0)
        k = tmp.pop(0)
        G[u] = tmp
    d = bfs(G, 1)
    for node in G:
        print('{0} {1} '.format(node, d[node]))

Python: import queue

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

dist = [-1 for i in range(n)]
todo = queue.Queue()
seen = set()

dist[0] = 0
todo.put(1)

while (not todo.empty()):
  now = todo.get()
  for nx in graph[now]:
    if (dist[nx-1] != -1):
      continue
    dist[nx-1] = dist[now-1] + 1
    todo.put(nx)

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

Python: from collections import deque

n = int(input())
adj = [[]]
for i in range(n):
    adj.append(list(map(int, input().split()))[2:])

ans = [-1]*(n+1)
ans[1] = 0

q = deque([1])
visited = [False] * (n+1)
visited[1] = True

while q:
    x = q.popleft()
    for y in adj[x]:
        if visited[y] == False:
            q.append(y)
            ans[y] = ans[x]+1
            visited[y] = True

for j in range(1, n+1):
    print(j, ans[j])

Python: n = int(input())
adj = [list(map(int, input().split()))[2:] for _ in range(n)]
 
d = [-1] * n
 
s = 1
q = []
d[s - 1] = 0
q.append(s)
while len(q):
    u = q.pop()
    v = adj[u - 1]
    for vi in v:
        if d[vi - 1] == -1:
            d[vi - 1] = d[u - 1] + 1
            q.append(vi)
 
 
for i, di in enumerate(d):
    print(i + 1, di)

Python: from collections import deque
n = int(input())

G = []
G.append([])
for i in range(n):
    v = list(map(int, input().split()))
    G.append(v[2:])

q = deque()
q.append(1)

checked = [False] * (n + 1)
checked[1] = True
d = [-1] * (n + 1)
d[1] = 0

while q:
    current = q.pop()
    for v in G[current]:
        if not checked[v]:
            q.append(v)
            d[v] = d[current] + 1
            checked[v] = True

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