Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include<iostream>
#include<deque>
using namespace std;

static const int INFTY = 1<<21;

/*
enum Color{
    WHITE,
    GRAY,
    BLACK
};
*/

int n;
deque<int> Q;
int d[100]; //距離で色も表せる
bool M[100][100];
//Color color[100];


void dfs(int i){
    for(int j=0; j<n; j++){
        //color[i] = WHITE;
        d[j] = INFTY;   //色の要素も兼ねている
    }

    //color[i] = GRAY;
    d[i] = 0;
    Q.push_back(i);

    while(!Q.empty()){
        int u = Q.front();
        Q.pop_front();
        for(int v=0; v<n; v++){
            if((M[u][v]==true)&&(d[v]==INFTY)){
                //color[u] = GRAY;
                d[v] = d[u] + 1;
                Q.push_back(v);
            }
        }
        //color[u] = BLACK;
    }

    for(int i=0; i<n; i++){
        cout << i+1 << " ";
        if(d[i]==INFTY) cout << -1;
        else cout << d[i];
        cout << endl;
    }
}

int main(){

    cin >> n; 

    for(int i=0; i<n; i++){
        int u, k;
        cin >> u >> k;
        u--;
        for(int j=0; j<k; j++){
            int v;
            cin >> v;
            v--;
            M[u][v] = true;
        }
    }

    dfs(0);

    return 0;
}

C++: #include<iostream>
#include<vector>

int ptod[101]{};
int vertices_num;
std::vector<int> dest[101];
std::vector<int> visited_list;
void search();

int main()
{
	int vertex_tmp, dest_num, dest_tmp;

	std::cin >> vertices_num;
	for (int i = 1; i <= vertices_num; ++i)
	{
		std::cin >> vertex_tmp >> dest_num;
		for (int j = 0; j < dest_num; ++j)
		{
			std::cin >> dest_tmp;
			dest[vertex_tmp].push_back(dest_tmp);
		}
	}

	search();

	for (int i = 1; i <= vertices_num; ++i)
		std::cout << i << " " << ptod[i] << std::endl;

	return 0;
}

void search()
{
	int pre_size{ 0 };
	visited_list.push_back(1);
	bool visited[101]{ false, true };
	for (int i = 1; pre_size != visited_list.size(); ++i)
	{
		int now_size = visited_list.size();
		for (int j = pre_size; j < now_size; ++j)
			for (auto e : dest[visited_list[j]])
				if (!visited[e])
				{
					visited[e] = true;
					visited_list.push_back(e);
					ptod[e] = i;
				}
		pre_size = now_size;
	}
	for (int i = 1; i <= vertices_num; ++i)
		if (!visited[i])
			ptod[i] = -1;
	return;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n;
vector < int > G[105];
int mc[105];

void bfs()
{
    queue < int > que;

    fill_n(mc, 105, -1);
    que.push(1);
    mc[1] = 0;
    
    while ( !que.empty() ) {
        int u = que.front(); que.pop();
        for ( auto &v : G[u] ) {
            if ( mc[v] == -1 ) {
                mc[v] = mc[u] + 1;
                que.push(v);
            }
        }
    }
}

int main()
{
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        int u, k;
        cin >> u >> k;
        for ( int j = 0; j < k; j++ ) {
            int v;
            cin >> v;
            G[u].push_back(v);
        }
    }
    
    bfs();

    for ( int i = 1; i <= n; i++ ) {
        cout << i << " " << mc[i] << endl;
    }

    return (0);
}