Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	g := make([][]int, n+1) // 1-indexed
	d := make([]int, n+1)   // 1-indexed
	for i := 1; i <= n; i++ {
		d[i] = -1
		var u, k, v int
		fmt.Scan(&u, &k)
		for j := 0; j < k; j++ {
			fmt.Scan(&v)
			g[u] = append(g[u], v)
		}
	}

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

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);
}