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: n = int(input())
a = []
for i in range(n):
  tmp = list(map(int, input().split()))
  a.append(tmp[2:])
ans = [-1 for _ in range(n + 1)]
ans[1] = 0
que = [1]
while len(que) > 0:
  current = que.pop(0)
  if len(a[current - 1]) == 0:
    continue
  for next in a[current - 1]:
    if ans[next] == -1:
      que.append(next)
      ans[next] = ans[current] + 1
for i in range(1, n + 1):
  print(i, ans[i])

Python: N= int(input())
E = [input().split()[2:] for _ in [0]*N]
L,Q = [0] + [-1]*N, [0]
def func(ide):
    for i in E[ide]:
        i = int(i) - 1
        if L[i] < 0 :L[i] = L[ide] +1 ;Q.append(i) 
        else:return

while Q:top = Q.pop();func(top)

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

Python: #coding: UTF-8
from collections import deque
n = int(input())
link = [[] for i in range(n+1)]
for i in range(n):
  r = list(map(int,input().split()))
  u = r.pop(0)
  k = r.pop(0)
  link[u] = r

searched=set()
queue = deque()
d = [-1 for i in range(n+1)]

v = 1
d[v] = 0
queue.append(v)
searched.add(v)
while queue:
  v = queue.popleft()
  for w in link[v]:
    if w not in searched:
      d[w] = d[v] + 1
      queue.append(w)
      searched.add(w)

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

Python: n = int(input())

graphs = []

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


def bfs(graphs, seen_points, score, scores, queue):
    while len(queue) > 0:
        start_point = queue.pop(0)
        if seen_points[start_point - 1] == 2:
            continue
        else:
            next_points = [
                point for point in graphs[start_point - 1] if seen_points[point - 1] == 0]
            if len(next_points) >= 1:
                score += 1
                for point in next_points:
                    queue.append(point)
                    if seen_points[point - 1] == 0:
                        seen_points[point - 1] = 1
                        scores[point - 1] = scores[start_point - 1] + 1
                seen_points[start_point - 1] = 2


seen_points = [0 for _ in range(n)]
scores = [-1 for _ in range(n)]
scores[0] = 0
queue = [1]
bfs(graphs, seen_points, 0, scores, queue)

for index, score in enumerate(scores):
    print(index + 1, score)

Python: import sys
input = sys.stdin.readline

#入力と違って０オリジンとなっているので注意
n = int(input())
li = [[0] * n for _ in range(n)] #隣接行列、０オリジン li[i][j]:iからjへの有向グラフがある
for i in range(n):
    x, y, *v = [int(x)-1 for x in input().split()]
    for j in v:
        li[i][j] = 1 #有向グラフがあるところは１、ないところは０となる

from collections import deque
Q = deque([0]) #初期条件
finish = [0] #訪問済みの頂点をしまう
len_ = [-1]*n #頂点jの頂点０からの距離、できなかったらー１と返す
len_[0] = 0 #初期条件
while Q: #Qがからでない限り以下の操作を繰り返す
    i = Q.popleft()
    for j in range(n):
        if li[i][j] == 1: #iからjへの道が存在する場合
            if not j in finish: #まだjが探索済みじゃなかったら以下の操作を行う
                finish.append(j)
                Q.append(j)
                len_[j] = len_[i] + 1
        else:
            continue
for i in range(n):
    print(str(i+1) + ' ' + str(len_[i]))

Python: n = int(input())
graph = {}
d = [-1] * (n+1)
for i in range(n):
    t = list(map(int, input().split()))
    v = t[0]
    k = t[1]
    graph[v] = [t[i+2] for i in range(k)]

from queue import Queue

def bfs(s):
    d[s] = 0
    q=Queue()
    q.put(s)
    while not q.empty():
        v=q.get()
        for child in graph[v]:
            if d[child] != -1: continue
            d[child] = d[v] + 1
            q.put(child)
bfs(1)
for i in range(1, n+1):print(i,d[i])

Python: from sys import stdin
from collections import deque
n = int(stdin.readline())
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] < 0:
                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, stdin.readline().split()[2:])) for i in range(n)]
bfs()

Python: N= int(input())
E = [input().split()[2:] for _ in range(N)]
Q = [0]
L = [0] + [-1]*N
frag = [1] + [0]*N
def func(ide):
    global Q
    for i in E[ide]:
        i = int(i) - 1
        if frag[i] != 1:frag[i] = 1;L[i] = L[ide] +1 ;Q.append(i)
        else:return None
    

while Q:top = Q[0];func(top);del Q[0]

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

Python: n = int(input())

adj = [None]
for i in range(n):
    adj_i = list(map(int, input().split()[2:]))
    adj.append(adj_i)

isSearched = [None] + [False] * n

distance = [None] + [-1] * n

import collections

def bfs(u):
    isSearched[u] = True
    distance[u] = 0
    edge = collections.deque()
    edge.append(u)
    while edge:
        c_e = edge.popleft()
        for n_e in adj[c_e]:
            if not isSearched[n_e]:
                isSearched[n_e] = True
                distance[n_e] = distance[c_e] + 1
                edge.append(n_e)


bfs(1)

for i, x in enumerate(distance[1:], start=1):
    print(i, x)

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys


def bfs(matrix):
    distances = [-1 for _ in range(len(matrix))]
    queue = [0]
    distances[0] = 0

    while 0 < len(queue):
        node_id = queue.pop(0)  # Dequeue
        distance = distances[node_id]
        for index, candidate in enumerate(matrix[node_id]):
            if candidate == 1 and distances[index] == -1:
                # ???????????¢????¨?????????????????????? node
                queue.append(index)
                distances[index] = distance + 1

    return distances

if __name__ == "__main__":
    # ??????????????????
    # lines = [
    #     "4",
    #     "1 2 2 4",
    #     "2 1 4",
    #     "3 0",
    #     "4 1 3",
    # ]
    lines = sys.stdin.readlines()
    dimension = int(lines[0])
    matrix = [[0 for _ in range(dimension)] for _ in range(dimension)]

    # Set edge info
    for index, line in enumerate(lines[1:]):
        for edge in [int(x) for x in line.strip().split(" ")[2:]]:
            matrix[index][edge - 1] = 1

    distances = bfs(matrix)
    for index, distance in enumerate(distances):
        print("%d %d" % (index + 1, distance))