Go: package main

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

type vertexId int

type vertex struct {
	id vertexId
	visitedAt int
	adjs []vertexId
}

func createVertex(id int, adjsInt []int) vertex {
	adjs := make([]vertexId, len(adjsInt))
	for i := 0; i < len(adjs); i++ {
		adjs[i] = vertexId(adjsInt[i])
	}
	return vertex{
		id: vertexId(id),
		visitedAt: -1,
		adjs: adjs,
	}
}

func (v *vertex) isVisited() bool {
	return v.visitedAt >= 0
}

func bfs(id vertexId, vqueue []vertexId, vertices []vertex) {
	vertex := &vertices[id-1]
	for _, vid := range vertex.adjs {
		if !vertices[vid-1].isVisited() {
			(&vertices[vid-1]).visitedAt = vertex.visitedAt+1
			vqueue = append(vqueue, vid)
		}
	}
	if len(vqueue) > 0 {
		nextId := vqueue[0]
		nextVqueue := vqueue[1:]
		bfs(nextId, nextVqueue, vertices)
	}
}

func solveBfs(vertices []vertex) {
	vertex := &vertices[0]
	vertex.visitedAt = 0
	vqueue := make([]vertexId, 0)
	bfs(vertex.id, vqueue, vertices)
}

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

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

	vertices := make([]vertex, n)
	for i := 0; i < n; i++ {
		id, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjN, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		adjs := make([]int, adjN)
		for j := 0; j < adjN; j++ {
			adjId, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjs[j] = adjId
		}
		vertices[i] = createVertex(id, adjs)
	}

	solveBfs(vertices)

	for i := 0; i < n; i++ {
		fmt.Printf("%d %d\n", vertices[i].id, vertices[i].visitedAt)
	}
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

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