Go: package main

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

func main() {
	scn := bufio.NewScanner(os.Stdin)
	scn.Split(bufio.ScanWords)
	nextInt := func() int { scn.Scan(); r, _ := strconv.Atoi(scn.Text()); return r }
	w := bufio.NewWriter(os.Stdout)

	n := nextInt()
	vecs := make([][]int, n)
	for i := 0; i < n; i++ {
		u := nextInt()
		k := nextInt()
		for j := 0; j < k; j++ {
			vecs[u-1] = append(vecs[u-1], nextInt()-1)
		}
	}

	ds := make([]int, 0, n)
	for i := 0; i < n; i++ {
		ds = append(ds, -1)
	}

	q := make([]int, 0, n)
	q = append(q, 0)
	ds[0] = 0

	for len(q) > 0 {
		i := pop(&q)
		for _, v := range vecs[i] {
			if ds[v] == -1 {
				ds[v] = ds[i]+1
				q = append(q, v)
			}
		}
	}

	for i, d := range ds {
		fmt.Fprintln(w, i+1, d)
	}

	w.Flush()
}

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

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)

Python: from collections import deque

def bfs(adj_mat, d):
    Q = deque()
    N = len(d)
    color = ["W" for n in range(N)]

    Q.append(0)
    d[0] = 0

    while len(Q) != 0:
        u = Q.popleft()

        for i in range(N):
            if adj_mat[u][i] == 1 and color[i] == "W":
                color[i] = "G"
                d[i] = d[u] + 1
                Q.append(i)

        color[u] = "B"

    for i in range(N):
        if color[i] != "B":
            d[i] = -1

def Main():
    N = int(input())

    adj_mat = [[0 for n in range(N)] for n in range(N)]
    d = [0] * N

    for n in range(N):
        u, k , *V = map(int, input().split())

        if k > 0:
            for i in V:
                adj_mat[n][i - 1] = 1

    bfs(adj_mat, d)

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

Python: if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    from collections import deque, defaultdict
    from copy import deepcopy

    INFTY = -1   # 探索しなかったやつを-1としてprintしやすくするために-1とした。

    n = int(input())

    M = [[0]*n for _ in range(n)]

    def bfs(s):
        q = deque()
        q.append(s)
        d = [INFTY]*n
        d[s] = 0
        while q: # qが空だとwhileループから抜ける
            u = q.popleft()  # pythonのdequeはpopleft関数で戻り値がpopした要素になる
            for v in range(n):
                if M[u][v] == 0:
                    continue
                if d[v] != INFTY:
                    continue
                d[v] = d[u] + 1
                q.append(v)
        for i in range(n):
            print(str(i+1) + ' ' + str(d[i]))

    for _ in range(n):
        u, k, *vv = map(int, input().split())
        u -= 1
        for v in vv:
            v -= 1
            M[u][v] = 1

    bfs(0)

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