Go: package main

/*
グラフの表現 隣接行列による表現
*/

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

func main() {
	scanner := makeScanner(8192)
	n := eGetInt(scanner)
	g := make([][]int, n+1)
	for i := 1; i < n+1; i++ {
		g[i] = make([]int, n+1)
		ints := eGetInts(scanner)
		num := ints[0]
		for j := 0; j < ints[1]; j++ {
			g[num][ints[j+2]] = 1
		}
	}
	dist := make([]int, n+1)
	for i := 0; i < len(dist); i++ {
		dist[i] = -1
	}
	q := []int{}

	// 訪問する
	q = append(q, 1)
	dist[1] = 0

	// キューが空でない限り
	for len(q) > 0 {
		// 先頭を取り出す
		top := 0
		top, q = q[0], q[1:]
		// 隣接マスを入れる.
		for i := 1; i < n+1; i++ {
			// 辺がある
			if g[top][i] == 1 {
				if dist[i] == -1 {
					// 訪問済みでなければ
					q = append(q, i)
					dist[i] = dist[top] + 1
				}
			}
		}
	}
	for i := 1; i < n+1; i++ {
		fmt.Println(i, dist[i])
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetInt64(r *bufio.Scanner) int64 {
	line := eGetLine(r)
	return eAtoInt64(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetInt64s(r *bufio.Scanner) []int64 {
	fields := eGetFields(r)
	ints := make([]int64, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoInt64(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}
func eAtoInt64(s string) int64 {
	n, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

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