Go: package main

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

const MaxNode int = 100

var d []int
var A [][]int
var N int
var l *list.List

func bfs(u int) {
	d[u] = 0
	l.PushBack(u)
	for ;l.Len() != 0; {
		u = l.Front().Value.(int)
		l.Remove(l.Front())
		for v:=0; v<N; v++ {
			if A[u][v] == 0 {
				continue
			}
			if d[v] != -1 {
				continue
			}
			d[v] = d[u] + 1
			l.PushBack(v)
		}
	}
}


func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func print() {
	for i:=0; i<N; i++ {
		fmt.Printf("%d %d\n", i+1, d[i])
	}
}

func initVariable() {
	d = make([]int, MaxNode)
	A = make([][]int, MaxNode)
	l = list.New()
	for i:=0; i<MaxNode; i++ {
		A[i] = make([]int, MaxNode)
		d[i] = -1
	}
}


func main() {
	initVariable()
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	for i:=0; i<N; i++ {
		idx := nextInt(scanner)
		cnt := nextInt(scanner)
		for j:=0; j<cnt; j++ {
			node := nextInt(scanner)
			A[idx-1][node-1] = 1 
		}
	}
	bfs(0)
	print()
}

Python: def breadth_first_search(G):
  n = len(G)
  d = [-1] * (n + 1)
  d[1] = 0
  queue = [1]

  while len(queue) > 0:
    v = queue.pop(0)
    for c in G[v]:
      if d[c] < 0:
        d[c] = d[v] + 1
        queue.append(c)

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

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

breadth_first_search(G)

Python: n = int(input())
g = [[0]*(n+1) for _ in range(n+1)]
for i in range(n):
    u, k, *v, = map(int, input().split())
    for vi in v:
        g[u][vi] = 1

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

l = [1]
while len(l) > 0:
    u = l.pop(0)
    for i, v in enumerate(g[u]):
        if v == 1 and d[i] < 0:
            d[i] = d[u]+1
            l.append(i)

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

Python: import sys
import itertools
# import numpy as np
import time
import math
import heapq
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
# list(map(int, input().split()))

n = int(input())

adj = [[] for i in range(n)]

for i in range(n):
    line = list(map(int, input().split()))
    u = line[0]
    for j in range(line[1]):
        v = line[2 + j]
        adj[u - 1].append(v - 1)


# BFS
import queue
q = queue.Queue()
visited = [False] * n
dist = [-1] * n

visited[0] = True
dist[0] = 0
q.put(0)
while not q.empty():
    v = q.get()
    for u in adj[v]:
        if visited[u]:
            continue
        visited[u] = True
        dist[u] = dist[v] + 1
        q.put(u)

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

Python: # ALDS_11_C - 幅優先探索
import sys
import heapq

N = int(input())
G = []
for _ in range(N):
    u, k, *v = map(int, sys.stdin.readline().strip().split())
    v = [i - 1 for i in v]
    G.append(v)


distance = [-1] * N
q = []
fp = [True] * N  # 探索可能か記録する
heapq.heapify(q)
heapq.heappush(q, (0, 0))
fp[0] = False


while q:
    dist, u = heapq.heappop(q)
    distance[u] = dist
    for v in G[u]:
        if fp[v] is True:
            heapq.heappush(q, (dist + 1, v))
            fp[v] = False

for i, d in enumerate(distance):
    print(i + 1, d)

Python: from queue import Queue

n = int(input())
A = [[0 for i in range(n)] for j in range(n)]
d = [-1 for i in range(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()
    # print('visited:', u)
    for v in range(n):
        # print(u, v)
        if A[u][v] == 1 and d[v] == -1:
            q.put(v)
            d[v] = d[u] + 1

# print('-========')
for i in range(n):
    print(i+1, d[i])

Python: from typing import List
from collections import deque

def bfs(G: List[int], s: int) -> None:
	d[s] = 0
	color[s] = "GRAY"
	Q = deque()
	Q.append(s)
	while Q:
		u = Q.popleft()
		for v in G[u]:
			if color[v] == "WHITE":
				color[v] = "GRAY"
				d[v] = d[u] + 1
				pi[v] = u
				Q.append(v)
		color[u] = "BLACK"


if __name__ == "__main__":
	n = int(input())
	G = [[] for _ in range(n)]
	for _ in range(n):
		u, k, *adj = list(map(int, input().split()))
		u -= 1
		G[u] = [v - 1 for v in adj]
	color = ["WHITE" for _ in range(n)]
	d = [-1 for _ in range(n)]
	pi = [None for _ in range(n)]
	bfs(G, 0)
	for i in range(n):
		print(f"{i + 1} {d[i]}")

Python: from functools import lru_cache

@lru_cache(maxsize=None)
def solve(i, count):
    global ans
    global a
    if count >= N:
        return
    if ans[i][0] == -1:
        ans[i][0] = count
    elif ans[i][0] > count:
        ans[i][0] = count

    for x in range(a[i][1]):
        solve(a[i][2 + x] - 1, count + 1)


N = int(input())
a = []
for _ in range(N):
    a.append([int(x) for x in input().split()])

ans = [[-1 for i in range(1)] for j in range(N)]

solve(0, 0)

for i, x in enumerate(ans):
    print(i + 1, *x)

Python: from collections import deque
from sys import stdin
input = stdin.readline

N = int(input())
neighborlist = [None for _ in range(N+1)]
for _ in range(1, N+1):
    node, _, *L = list(map(int, input().split()))
    neighborlist[node] = L

distancelist = [-1]*(N+1)
queue = deque()
queue.append(1)
distancelist[1] = 0
while queue:
    node = queue.popleft()
    distance = distancelist[node]
    for neighbor in neighborlist[node]:
        if distancelist[neighbor] == -1:
            distancelist[neighbor] = distance + 1
            queue.append(neighbor)

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

Python: from collections import deque
V = int(input())
edge = [[] for _ in range(V)]
for _ in range(V):
    u, _, *v = map(lambda x: int(x)-1, input().split())
    edge[u] = sorted(v)
dist = [-1] * V
dist[0] = 0
que = deque([0])

while len(que):
    v = que.popleft()
    for c in edge[v]:
        if dist[c] == -1:
            dist[c] = dist[v] + 1
            que.append(c)
for i, d in enumerate(dist):
    print(i+1, d)

Python: from collections import deque

def bfs(s, n, m):
    q = deque()
    q.append(s)
    INFTY = 1 << 21
    d = [INFTY] * n
    d[s] = 0
    while len(q) != 0:
        u = q[0]
        q.popleft()
        for v in range(n):
            if not m[u][v]:
                continue
            if d[v] != INFTY:
                continue
            d[v] = d[u] + 1
            q.append(v)
    for i in range(n):
        result = str(i+1)
        if d[i] == INFTY:
            result += ' -1'
        else:
            result += ' %d' % d[i]
        print(result)


n = int(input())
m = [[False] * n for i in range(n)]
for i in range(n):
    line = [int(v) for v in input().split()]
    u = line[0] - 1
    for v in line[2:]:
        v -= 1
        m[u][v] = True

bfs(0, n, m)

Python: import queue

N = int(input())
G = []
for _ in range(N):
    u, n, *K = map(int, input().split())
    G.append((u - 1, [k - 1 for k in K]))

Q = queue.Queue()
Q.put(G[0])
dist = [-1] * N
dist[0] = 0
while not Q.empty():
    U = Q.get()
    for u in U[1]:
        if dist[u] == -1:
            Q.put(G[u])
            dist[u] = dist[U[0]] + 1

for i, time in enumerate(dist):
    print(i + 1, time)

Python: import sys

n = int(sys.stdin.readline())
dist = [0] + [-1] * (n-1)
vv=[list(map(int, sys.stdin.readline().split()))[2:] for _ in range(n)]


def walk(k, d):
    for i in vv[k]:
        if dist[i-1]<0 or (dist[i-1]>0 and dist[i-1]>d):
            dist[i-1]=d
        else: continue
        walk(i-1, d+1)

walk(0, 1)

for i, d in enumerate(dist):
    print(i+1, d)