Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

Python: from collections import deque
def bfs(g,v):
    q=deque([v]) ; vis=[0]*n
    vis[v]=1 ; stg[v]=0
    while q:
        v=q[0]
        for p in g[v]:
            if vis[p]==0: 
                q.append(p)
                vis[p]=1
                stg[p]=stg[v]+1
        q.popleft()
n=int(input())
g=[[] for i in range(n)]
for _ in range(n):
    a=[int(i)-1 for i in input().split()]
    for i in a[2:]: g[a[0]].append(i)
stg=[-1]*n
bfs(g,0)
for i in range(n):
    print(i+1,stg[i])

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])