Go: package main

import (
	"container/list"
	"fmt"
	"bufio"
	"os"
	"strconv"
)

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

var (
	n        int
	adj      [N][N]int
	distance [N]int
)

func BFS(start int) {
	q := list.New()
	q.PushBack(start)
	for i := 0; i < n; i++ {
		distance[i] = INF
	}
	distance[start] = 0
	u := 0
	for q.Len() != 0 {
		u = q.Remove(q.Front()).(int)
		for v := 0; v < n; v++ {
			if adj[u][v] == 0 {
				continue
			}
			if distance[v] != INF {
				continue
			}
			distance[v] = distance[u] + 1
			q.PushBack(v)
		}
	}
	for i := 0; i < n; i++ {
		var d int
		if distance[i] == INF {
			d = -1
		} else {
			d = distance[i]
		}
		fmt.Printf("%d %d\n", i+1, d)
	}
}

func main() {
	var u, k, v int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			adj[i][j] = 0
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		u, _ = strconv.Atoi(scanner.Text())
		u -= 1
		scanner.Scan()
		k, _ = strconv.Atoi(scanner.Text())
		for j := 0; j < k; j++ {
			scanner.Scan()
			v, _ = strconv.Atoi(scanner.Text())
			v -= 1
			adj[u][v] = 1
		}
	}
	BFS(0)
}

Python: from collections import deque

N = 100
n = 0
M = None
WHITE = 0
GRAY = 1
BLACK = 2
INF = float('inf')
d = None


def main():
    global n, M, d
    M = [[0 for _ in range(N)] for _ in range(N)]
    n = int(input())
    d = [INF for _ in range(n)]

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

    bfs()

    for i in range(n):
        print('{} {}'.format(i+1, d[i] if d[i] != INF else -1))


def bfs():
    q = deque()
    q.appendleft(0)
    d[0] = 0

    while len(q) > 0:
        u = q.pop()
        for v in range(n):
            if M[u][v] == WHITE:
                continue
            if d[v] != INF:
                continue
            d[v] = GRAY
            d[v] = d[u] + 1
            q.appendleft(v)


if __name__ == '__main__':
    main()

Python: # -*- coding: utf-8 -*-

import sys
import os
import math

N = int(input())

E = []
E.append([])
d = [None] * (N + 1)

for i in range(N):
    lst = list(map(int, input().split()))
    nodes = lst[2:]
    E.append(nodes)
#print(E)

q = []
q.append(1)

visited = [False] * (N + 1)
d[1] = 0
visited[1] = True

while q:
    index = q.pop(0)

    # ???????????????????????´???
    for v in E[index]:
        if not visited[v]:
            q.append(v)
            d[v] = d[index] + 1
            visited[v] = True

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

Python: import collections
n= int(input())
g=[]
for _ in range(n):
    v,k,*varray= map(int,input().split())
    g.append(varray)
        

d= [-1]* (n+10)
d[0]=0
q= collections.deque()
q.append(0)
while len(q)>  0:
    cur = q.popleft()
    for next in g[cur]:
        if d[next-1]== -1:
            d[next-1]= d[cur]+1
            q.append(next-1)
            
for i in range(n):
    print(i+1,d[i])

Python: n=int(input())
d=[0]+[float('inf')]*(n-1)

v=[[]]*n
for i in range(n):
    tmp=list(map(int,input().split()))
    tmp=[i-1 for i in tmp[2:]]
    v[i]=tmp

que=[0]
while que!=[]:
    m=que.pop(0)
    for i in v[m]:
        if d[i]==float('inf'):
            que.append(i)
            d[i]=d[m]+1
for i in range(n):
    if d[i]==float('inf'):
        d[i]=-1
    print(i+1,d[i])

Python: from collections import deque


def bfs(g, source):
    n = len(g)
    d = [float('inf') for _ in range(n)]
    d[0] = 0
    queue = deque()
    queue.append(0)
    while len(queue) > 0:
        cur = queue.popleft()
        for next in g[cur]:
            if d[cur] + 1 < d[next]:
                d[next] = d[cur] + 1
                queue.append(next)
    return d


def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n):
        inp = list(map(int, input().split()))
        m = inp[0] - 1
        k = inp[1]
        for i in range(k):
            g[m].append(inp[i+2] - 1)
    d = bfs(g, 0)
    for i in range(n):
        if d[i] == float('inf'):
            d[i] = -1
        print(i+1, d[i])


if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from collections import deque,defaultdict,Counter
def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N):
        A = list(map(int,input().split()))
        for a in A[2:]:
            a -= 1
            G[i].append(a)
    
    q = deque([0])
    dist = [-1] * N
    dist[0] = 0
    while q:
        v = q.popleft()
        for e in G[v]:
            if dist[e] >= 0:
                continue
            dist[e] = dist[v] + 1
            q.append(e)
    
    ans = [(i + 1,dist[i]) for i in range(N)]
    for t in ans:
        print(*t)
if __name__ == '__main__':
    main()

Python: from collections import deque
n=int(input())
E=[0]*n
for _ in range(n):
    u,k,*V=map(int,input().split())
    E[u-1]=V
d=[-1]*n
d[0]=0
todo=deque([1])
while todo:
    now=todo.popleft()
    for v in E[now-1]:
        if d[v-1]==-1:
            todo.append(v)
            d[v-1]=d[now-1]+1
for i in range(n):
    print(i+1,d[i])

Python: from sys import stdin
from collections import deque

n = int(stdin.readline())
g = [None] * n
for _ in range(n):
    u, _, *cs = [int(s) for s in stdin.readline().split()]
    g[u-1] = [c - 1 for c in cs]
ds = [-1] * n
v = [False] * n
v[0] = True
q = deque([(0, 0)])
while len(q):
    u, d = q.popleft()
    ds[u] = d
    for c in g[u]:
        if not v[c]:
            v[c] = True
            q.append((c, d + 1))
for u, d in enumerate(ds):
    print(u + 1, d)

Python: from collections import deque

N = int(input())                            # 頂点数

conj = [[] for _ in range(N)]
for i in range(N):                          # 各頂点での進める先の候補の集合
    L = list(map(int, input().split()))
    for a in L[2:]:
        conj[i].append(a-1)         # 頂点番号をデクリメント

start = 0

visit = [False]*N                           # 訪れたかどうかをメモ
second_visit = [False]*N                    # 二度目に訪れた事をメモ
next_set = deque()                          # 次に進む候補を列挙する。スタック（右から取り出す = pop）だと深さ優先になり、キュー（左から取り出す = popleft）だと幅優先になる
next_set.append((start, 0))                 # スタート地点を決める。第二成分は時刻を表す。
visit[start] = True                         # スタート地点は最初から踏んでいる。
res = [-1 for _ in range(N)]
res[0] = 0

while next_set:                             # p = [] になったら止める
    p, t = next_set.popleft()               # 要素を消去して、消去した要素を出力
    for q in conj[p]:                       # 頂点 p から進める経路の候補から一つ選ぶ
        if not visit[q]:                    # 訪れた事がない場所のみ進む。壁などの条件がある場合は、ここに " grid[p] != 壁 " 等を追加する
            visit[q] = True                 # 頂点 q に一度訪れた事をメモ (for の前に書くと、ここで選ばれた q が最短であるはずなのに、違う経路でvisit = Trueを踏んでしまう可能性がある)
            res[q] = t + 1
            next_set.append((q, t + 1))     # p から q へと移動。時刻を 1 進める


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

Python: from collections import deque
n = int(input())
A = [0]*(n+1)
D = [0]*(n+1)
DP = [0]*(n+1)
d = deque()
for i in range(n):
    A[i+1] = [int(i) for i in input().split()]
d.append(1)
DP[1]=0
while(len(d)>0):
    tmp = d.pop()
    for i in A[tmp][2:]:
        if not DP[i]:
            D[i] = D[tmp]+1
            DP[i] = 1
            d.appendleft(i)
for i in range(1,n+1):
    tmp = "{} {}".format(i,D[i])
    print(tmp)