Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

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

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

Python: from collections import deque

def main():

    inf = 1000000007
    n = int(input())
    e = [[] for _ in range(n)]
    d = [inf]*n

    for i in range(n):
        m = list(map(int,input().split()))
        for j in range(2,len(m)):
            e[m[0]-1].append(m[j]-1)

    d[0] = 0
    dq = deque([])
    dq.append([0,0])
    while dq:
        c,v = dq.popleft()
        for u in e[v]:
            if d[u]==inf:
                d[u] = c+1
                dq.append([c+1,u])

    for i in range(n):
        if d[i]==inf:print (i+1,-1)
        else        :print (i+1,d[i])

if __name__ == '__main__':
    main()

Python: n=int(input())
ps=[0 for j in range(n+1)]

for i in range(n):
    p,d,*vs=[int(j) for j in input().split()]
    ps[p]=vs
    
dist=[-1 for i in range(n+1)]    
    
d=0
queue=[1]

while queue:
    pos=queue.pop(0)
    if dist[pos] == -1:
        dist[pos]=d
    for i in ps[pos]:
        if dist[i]==-1:
            queue.append(i)
            dist[i]=dist[pos]+1

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

Python: from collections import deque

n = int(input())
u = [[] for i in range(n+1)] #隣接リスト
for i in range(n):
    v = list(map(int, input().split()))
    u[v[0]] = v[1:] #v = [次数, 頂点...]

d = [-1] * (n+1)
visited = [False] * (n+1)

d[1] = 0
visited[1] = True
que = deque([1])
while len(que) > 0:
    c = que.popleft()
    for i in range(1, u[c][0]+1):
        if not visited[u[c][i]]:
            que.append(u[c][i])
            visited[u[c][i]] = True
            d[u[c][i]] = d[c] + 1

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

Python: from collections import deque

n = int(input())
graph_W = []
for _ in range(n):
    graph_W.append(list(map(int, input().split())))
    
num_root = [0]*n
work_queue = deque([])
visited = set()

work_queue.append(1)
visited.add(1)
cnt = 0
while work_queue:
    here = work_queue.popleft() - 1
    num_node = graph_W[here][1]
    cnt += 1
    if num_node == 0:
        continue
    for k in range(num_node):
        num_graph = graph_W[here][k+2]
        if num_graph not in visited:
            work_queue.append(num_graph)
            visited.add(num_graph)
            num_root[num_graph-1] = num_root[here] + 1


print(1,0)
for i in range(1,n):
    if num_root[i] != 0:
        print(i+1,num_root[i])
    else:
        print(i+1,-1)

Python: from collections import deque

INF = 1e9

def BFS(n, adj):
    d = [INF] * N
    d[0] = 0
    next_v = deque([0]) # ?¬??????????????????????
    
    while next_v:
        u = next_v.popleft()
        
        for v in adj[u]:
            if d[v] == INF:
                d[v] = d[u] + 1
                next_v.append(v)
    
    return d

N = int(input())
Adj = [None] * N

for i in range(N):
    u, k, *vertex = [int(i)-1 for i in input().split()]
    Adj[u] = vertex

distance = BFS(N, Adj)

for i, di in enumerate(distance, start = 1):
    if di == INF:
        print(i, -1)
    else:
        print(i, di)

Python: N= int(input())
E = [input().split()[2:] for _ in [0]*N]
Q = [0]
L = [0] + [-1]*N
def func(ide):
    for i in E[ide]:
        i = int(i) - 1
        if L[i] < 0 :L[i] = L[ide] +1 ;Q += [i]


while Q:top = Q.pop();func(top)

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

Python: 
def BFS(s = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(color)):
        if A[s][j] == 1 and color[j] == 0:
            Q.append(j)
            color[j] = 1
            d[j] = d[s] + 1
    if len(Q) != 0:
        BFS(Q[0])

n = int(raw_input())
A = [0] * n
for i in range(n):
    A[i] = [0] * n

for i in range(n):
    value = map(int, raw_input().split())
    u = value[0] - 1
    k = value[1]
    nodes = value[2:]
    for j in range(k):
        v = nodes[j] - 1
        A[u][v] = 1

color = [0] * n
Q = [0]
d = [-1] * n
d[0] = 0

BFS(0)

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

Python: from sys import stdin

X = [ 0 for i in range(100) ]
d = [ 0 for i in range(100) ]
G = [[ 0 for i in range(100) ] for i in range(100)]
queue = [ 0 for i in range(100) ]

t = 0
head = 0
tail = 0

def BFS(i) :
	global head
	for j in range(n) :
		if G[i][j] == 1 and d[j] == 0 :
			d[j] = d[i] + 1
			queue[head] = j
			head += 1

def tailQ() :
	global tail
	tail += 1
	return queue[tail-1]


n = int(input())
for i in range(n) :
	X[i] = stdin.readline().strip().split()


for i in range(n) :
	for j in range(int(X[i][1])) :
		G[int(X[i][0])-1][int(X[i][j+2])-1] = 1

BFS(0)
while head-tail != 0 : 
	BFS(tailQ())

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

Python: from collections import deque


def main():
    while que:
        v, d = que.popleft()
        for nv in G[v]:
            nv -= 1
            if D[nv] == -1:
                D[nv] = D[v] + 1
                que.append((nv, D[nv]))
    for i in range(N):
        print(i+1, D[i])


if __name__ == "__main__":
    N = int(input())
    G = [[] for _ in range(N)]
    for _ in range(N):
        ipts = [int(ipt) for ipt in input().split()]
        G[ipts[0]-1] = ipts[2:]
    D = [-1]*N
    D[0] = 0
    que = deque()
    que.append((0, 0))
    main()

Python: #!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function
import time
from sys import stdin
from Queue import Queue
start = time.clock()
i = 0
num=int(stdin.readline())
L=[]
for _ in range(num):
    L.append([int(s) for s in stdin.readline().split()[2:]])

d=[-1]*num
d[0]=0
q=Queue(100)
q.put(0)
while not q.empty():
    u=q.get()
    for v in L[u]:
        if d[v-1]<0:
            d[v-1]=d[u]+1
            q.put(v-1)
for i,v in enumerate(d):
    print (i+1,v)

Python: from collections import deque
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]


dist = [-1]*n #全頂点を未訪問に初期化(-1)で未訪問
que = deque([0])   #初期条件 (頂点 0 を初期ノードとする) 
dist[0] = 0 #0を訪問済み

while len(que)>0:
    now = que.popleft()
    for i in G[now]:
        if dist[i] != -1:
            continue
        dist[i] = dist[now] + 1 #新たな頂点wについて距離情報を更新してキューに追加する
        que.append(i)

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

Python: # Breadth First Search
from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
E = (tuple(map(int, input().split())) for _ in range(n))
E = [[v-1 for v in V] for i, k, *V in E]

q = deque()
visited = [False]*n
D = [-1]*n

# 初期条件
s = 0
q.append(s)
visited[s] = True
D[s] = 0

# BFS
while q:
    v = q.popleft()
    for u in E[v]:
        if not visited[u]:
            q.append(u)
            visited[u] = True
            D[u] = D[v]+1


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