Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
type Queue struct {
	arr []int
}

func (q *Queue) push(v int) {
	q.arr = append(q.arr, v)
}

func (q *Queue) pop() int {
	res := q.arr[0]
	q.arr = q.arr[1:]
	return res
}

func (q *Queue) isEmpty() bool {
	return len(q.arr) == 0
}

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		u, k := sc.nextInt()-1, sc.nextInt()
		for j := 0; j < k; j++ {
			G[u] = append(G[u], sc.nextInt()-1)
		}
	}
	var q Queue
	seen := make([]int, N)
	for i := 0; i < N; i++ {
		seen[i] = -1
	}
	var search func(cur int)
	search = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if seen[next] == -1 {
				q.push(next)
				seen[next] = seen[cur] + 1
			}
		}
	}
	q.push(0)
	seen[0] = 0
	for !q.isEmpty() {
		search(q.pop())
	}

	for i := 0; i < N; i++ {
		fmt.Fprintln(wtr, i+1, seen[i])
	}
	wtr.Flush()
}

Python: def BFS(s, r):
    d[s] = r
    for i in range(1, n+1):
        if G[s][i] == 1 and flag[i] == 0:
            BFS(i, r+1)

n = int(raw_input())
G = [[0 for i in range(n+1)] for j in range(n+1)]
d = [-1 for i in range(n+1)]

for i in range(n):
    v = map(int, raw_input().split())
    for j in range(v[1]):
        G[v[0]][v[2+j]] = 1

flag = [0 for i in range(n+1)]
BFS(1, 0)

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

Python: n = int(raw_input())

G = [0 for i in range(n)] 
a = [[0 for i in range(n)] for j in range(n)]
dis = [-1 for i in range(n)]

for i in range(n):
    G = map(int, raw_input().split())
    for j in range(G[1]):
        a[G[0]-1][G[2+j]-1] = 1

def bfs_visit(s):
    for k in range(n):
      if a[s][k] == 1 and dis[k] == -1:
         dis[k] = dis[s] + 1
         bfs_visit(k)

for s in range(s):
    bfs_visit()
for s in range(s):
    print (s+1,dis[s])

Python: import sys
from collections import deque

n = int(sys.stdin.readline().strip())
edges = [[] for _ in range(n)]
for _ in range(n):
    tmp = list(map(int, sys.stdin.readline().strip().split(" ")))
    for node in tmp[2:]:
        edges[tmp[0] - 1].append(node-1)
# print(edges)

distance = [0] * n
q = deque()
q.append((0, 0))
visited = set()
while q:
    node, d = q.popleft()
    # print(node, d)
    if node in visited:
        continue
    distance[node] = d
    visited.add(node)
    for next in edges[node]:
        # print("next", next)
        q.append((next, d + 1))

for i, d in enumerate(distance):
    if i == 0:
        print(1, 0)
    else:
        print(i + 1, d if d > 0 else -1)

Python: import collections

def breadth_first_search(g,check,length):
    q = collections.deque()
    q.append(1)
    check[1] = 1
    length[1] = 0
    while len(q) != 0:
        v = q.popleft()
        for u in g[v]:
            if check[u] == 0:
                check[u] = 1
                length[u] = length[v]+1
                q.append(u)

    return length

def main():
    N = int(input())
    g = []
    g.append([])
    for i in range(N):
        tmp = input().split()
        tmp_ls = []
        for i in range(int(tmp[1])):
            tmp_ls.append(int(tmp[i+2]))
        g.append(tmp_ls)

    check = [0]*(N+1)
    length = [0]*(N+1)
    ans = breadth_first_search(g,check,length)
    for i in range(N):
        node_num = i+1
        if ans[node_num] == 0 and i != 0:
            ans[node_num] = -1
        print(node_num,ans[node_num])

main()

Python: from collections import deque

def main():
    n = int(input())
    V = [None]*(n+1)
    ans = [None]*(n+1)
    for i in range(1, n+1):
        _, _, *v = map(int, input().split())
        V[i] = v
    q = deque([1])
    ans[1] = 0
    while q:
        node = q.pop()
        d = ans[node]+1
        for v in V[node]:
            if ans[v] is None:
                ans[v] = d
                q.appendleft(v)
    print(1, 0)
    for i, distance in enumerate(ans[2:], 2):
        print(i, distance or -1)

if __name__ == "__main__":
    main()

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(0);func(top)

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

Python: from collections import deque
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
d=deque()
d.append(1)
check=[0]*n
check[0]=1
ans=[0]*n
kyori=[0]*n
while len(d)!=0:
  v=d.popleft()
  for i in l[v-1][2:]:
    if check[i-1]==0:
      check[i-1]=1
      ans[i-1]=(kyori[v-1]+1)
      kyori[i-1]=(kyori[v-1]+1)
      d.append(i)
for i in range(1,n):
  if ans[i]==0:
    ans[i]=-1
for i in range(n):
    print(i+1,ans[i])

Python: # coding: utf-8

from queue import Queue

N = int(input())
graph = [None]
for _ in range(N):
    graph.append(list(map(int, input().split(" ")))[1:])

class BFSSolver:

    def __init__(self, graph):
        self.graph = graph
        self.queue = Queue()
        self.queue.put(1)
        self.before_id = 1
        self.already_searched = [1]
        self.res = [0]
        for _ in range(N):
            self.res.append(-1)
        self.res[1] = 0
        self.turn = 0

    def next_turn(self):
        now_id = self.queue.get()
        before_id = now_id
        neighbors = self.graph[now_id][1:]
        for i in neighbors:
            if i not in self.already_searched:
                self.turn = self.res[before_id] + 1
                self.already_searched.append(i)
                self.res[i] = self.turn
                self.queue.put(i)

    def solve(self):
        while not self.queue.empty():
            self.next_turn()

        for id, i in enumerate(self.res[1:]):
            print("{} {}".format(id+1, i))

s = BFSSolver(graph)
s.solve()

Python: #!/usr/bin/python3

def find(id, V, d, dist):
    i = id - 1
    dist[i] = d
    for v in V[i]:
        find(v, V, d + 1, dist)
    
n = int(input())
# [isFind, d, f]
A = [[False, 0, 0] for i in range(n)]
U = []
V = []
dist = [-1] * n
for i in range(n):
    l = list(map(int, input().split()))
    U.append(l[0])
    V.append(l[2:])

find(1, V, 0, dist)

for u in U:
    print(u, dist[u - 1])