Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
		nodes[i].d = -1
	}

	for i := 0; i < n; i++ {
		u := getNextInt(scanner)
		k := getNextInt(scanner)
		u--
		for ii := 0; ii < k; ii++ {
			v := getNextInt(scanner)
			v--
			nodes[u].list = append(nodes[u].list, v)
		}
	}

	queue := make([]int, n)
	queue[0] = 0
	nodes[0].d = 0
	d := 0
	i, j := 0, 1
	for i < j {
		d++
		prev := j
		for i < prev {
			for k, l := 0, len(nodes[queue[i]].list); k < l && i < n; k++ {
				if nodes[nodes[queue[i]].list[k]].d != -1 {
					continue
				}
				nodes[nodes[queue[i]].list[k]].d = d
				queue[j] = nodes[queue[i]].list[k]
				j++
			}
			i++
		}
	}

	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, fmt.Sprintf("%d %d", i+1, nodes[i].d))
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	d    int
}

Python: from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n = int(input())
d = [0 for i in range(n)]
f = [0 for i in range(n)]

colors = [] # 頂点の訪問状態
A = [[0 for j in range(n)] for i in range(n)] # 隣接行列
queue = deque([])

# 隣接行列の初期化
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.pop()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] == Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK

bfs()

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

Python: import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

N = int(input())
ukv = [list(map(int, input().split())) for _ in range(N)]
dist = [-1]*N
dist[0] = 0

deq = deque()
#deq.append(ukv[0][2:])
deq.append([1])

step = 0
while deq[0]:
#    print('deq:', deq)
    nextvs = []
    currvs = deq.popleft()
    for v in currvs:
        for nv in ukv[v-1][2:]:
            if dist[nv-1] == -1:
                dist[nv-1] = step+1
                nextvs.append(nv)
    deq.append(nextvs)
    step += 1

res = [[val1, val2] for val1, val2 in zip(range(1, N+1), dist)]
for a in res:
    print(*a)

Python: #coding:utf-8
n = int(input())
Q = [1]
M = [[False for i in range(n)] for j in range(n)]
dataList = []
for i in range(n):
    data = list(map(int,input().split()))
    dataList.append(data)
    for j in range(data[1]):
        v = data[j+2] - 1
        M[i][v] = True
color = ["white" for i in range(n)]
d = [-1 for i in range(n)]
d[0] = 0
while Q != []:
    u = Q.pop(0) - 1
    color[u] = "gray"
    data = dataList[u]
    for i in range(data[1]):
        v = data[i+2] - 1
        if M[u][v] == True and color[v] == "white":
            d[v] = d[u] + 1
            Q.append(v+1)
    color[u] = "black"

for i,j in enumerate(d,1):
    print(i,j)

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:
        if nodes[i].d == -1:
            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
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n = int(input())
d = [0 for i in range(n)]
f = [0 for i in range(n)]

colors = [] # 頂点の訪問状態
A = [[0 for j in range(n)] for i in range(n)] # 隣接行列
queue = deque([])

# 隣接行列の初期化
for i in range(n):
    arr = list(map(int, input().split(' ')))
    u, k, v = arr[0], arr[1], arr[2:]
    for j in range(k):
        A[u-1][v[j]-1] = 1

def bfs():
    global colors, queue, d
    colors = [Color.WHITE for i in range(n)]
    d = [-1 for i in range(n)] # INF

    colors[0] = Color.GRAY
    d[0] = 0
    queue.append(0)

    while len(queue) > 0:
        u = queue.popleft()
        for v in range(n):
            if A[u][v] == 1 and colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                d[v] = d[u] + 1
                queue.append(v)
        colors[u] = Color.BLACK

bfs()

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

Python: from collections import deque

n = int(input())
graph = [list(map(int,input().split()))[2:] for _ in range(n)]

dist = [-1]*(n+1)
dist[0] = 0
dist[1] = 0
d = deque()
d.append(1)

while d:
    v = d.popleft()-1
    if len(graph[v]) >= 1:
        for i in graph[v]:
            if dist[i] == -1:
                dist[i] = dist[v+1]+1
                d.append(i)

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

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp
# Breadth First Search: python3
#  2018.12.06 yonezawa

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

class node:
    def __init__(self):
        n = self.n = int(input())
        self.cnt = 0
        self.discover = [-1] * n
        self.pos = [1] * n
        self.nmap = [[0 for i in range(n)] for i in range(n)]
    
        for _ in range(n):
            l = list(map(int,input().split()))
            a = l[0] - 1
            for i in l[2:]:
                self.nmap[a][i-1] = 1
    def printMap(self):
        for i in self.nmap:
            print (*i)

    def printResult(self):
        for i in range(self.n):
            print(i+1,self.discover[i])

    def bfs(self,u):
        self.pos[u] = 0
        self.discover[u] = self.cnt
        t = deque()
        t.append(u)
        while t:
            n = t.popleft()
            for i in range(self.n):
                if (self.nmap[n][i] == 1 and self.pos[i] == 1 ):
                    t.append(i)
                    self.pos[i] = 0
                    self.discover[i] = self.discover[n] + 1
                
def main():
    a = node()
    a.bfs(0)
    a.printResult()
    #a.printMap()
if __name__ == '__main__':
    main()