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) {
			if !dl.isVisited(v) {
				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<vector>
#include<queue>
#include<algorithm>

int main() {

	int n;
	std::cin >> n;
	std::vector<std::vector<int>> v(n);

	for (int i{}; i < n; ++i) {
		int u, k;
		std::cin >> u >> k;
		for (int j{}; j < k; ++j) {
			int t;
			std::cin >> t;
			v[u - 1].emplace_back(t - 1);
		}
	}


	std::vector<int> dist(n, 1<<25 );
	dist[0] = 0;
//	for (int i{0}; i < n; ++i) {
		std::queue<int> q;
		q.emplace(0);
		while (!q.empty()) {
			auto here = q.front(); q.pop();
			
			for (const auto&e : v[here]) {
				if (e!=0&&dist[e] > dist[here] + 1) {
					dist[e] = dist[here] + 1;
					q.emplace(e);
				}
			}

		}
	

	for (int i{}; i < n; ++i) {
		std::cout
			<< i + 1<< " "
			<<( dist[i] == (1 << 25) ? -1 : dist[i] )<< "\n";
	}

	return 0; 
}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={0};
void bfs(int src,int n,int g[110][110]){
queue<int> Q;
Q.push(src);
D[src]=0;
while (! Q.empty()){
    int cur =Q.front();
    Q.pop();
    for(int r=0;r<n;r++){
        if (g[cur][r]==1&&D[r]==0){
            D[r]=D[cur]+1;
            Q.push(r);
        }
    }
}
}
int main() {
int n,g[110][110]={0};
cin>>n;
int j,num,t;
for(int k=0;k<n;k++){
cin>>j>>num;
for(int kk=0;kk<num;kk++){
    cin>>t;
    g[k][t-1]=1;
}
}
bfs(0,n,g);

for(int a=0;a<n;a++){
    if (D[a]==0){
        D[a]=-1;
}
    D[0]=0;
    cout<<a+1<<" "<<D[a]<<endl;
}
    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define N 100
#define INF (1<<21)
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define rep(i, n) for(int i = 0; i < (n); ++i)

int n, M[N][N];
int color[N], d[N];


void bfs(int r)
{
    //initialization
    rep(i, n) color[i] = WHITE;
    rep(i, n) d[i] = INF;

    queue<int> Q;
    Q.push(r);
    color[r] = GRAY;
    d[r] = 0;

    int u;
    while(!Q.empty()){
        u = Q.front(); Q.pop();
        for (int v = 0; v < n; ++v){
            if (M[u][v] != 0 && d[v] == INF){
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
    }
    for(int i = 0; i < n; ++i){
        cout << i+1 << " " << ((d[i] == INF)? -1 : d[i]) << endl;
    }

}

int main()
{
    int u, k, v;

    scanf("%d", &n);
    for(u = 0; u < n; ++u){
        scanf("%d %d", &u, &k);
        --u;
        for(int j = 0; j < k; ++j){
            scanf("%d", &v);
            --v;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}

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

// 頂点s+1から出発する場合の幅優先探索
void bfs(int s, int n, vector<vector<int>>& M, vector<int>& d) {
    static const int INFTY = (1 << 21);

    // 今回は標準ライブラリのキューを使う
    queue<int> q;

    // 各種初期化処理
    // 出発する頂点をpush
    q.push(s);
    // 距離格納配列の初期化
    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
    }
    // 出発点の距離は0
    d[s] = 0;

    // 探索開始
    int u;
    while (!q.empty()) {
        // 先頭を取り出してpopする(訪問)
        u = q.front();
        q.pop();

        // 隣接する訪問可能な頂点をpushする
        for (int v = 0; v < n; v++) {
            if (M[u][v] == 0) continue; //訪問不可
            if (d[v] != INFTY) continue; //訪問済み

            d[v] = d[u] + 1; //今回リンクのコストは1固定
            q.push(v);
        }
    }

    // 頂点1から各頂点までの距離を出力
    // INFTYが入っていた場合は到達不可能なので-1を出力
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}


int main() {
    static const int N = 110;
    // n : グラフの頂点数
    // M : 隣接行列(グラフを表現)
    // d : 任意の頂点から各頂点への距離格納配列(INFTYで初期化)
    int n, u, k, v;
    vector<int> d(N);
    vector<vector<int>> M(N, vector<int>(N));

    cin >> n;

    // グラフの初期化
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }
    // グラフの入力
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0, n, M, d);

    return 0;
}

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

const static int MAX = 100;
int R[MAX][MAX];
int color[MAX], ar[MAX];
int n;

int search(int i){
    std::queue<int> qu;
    if(color[i] == 1){
	return 1;
    }
    qu.push(i);
    color[i] = 1;
    ar[i] = 0;
    while(!qu.empty()){
	int cur = qu.front();
	int flag = 0;
	for(int j = 0; j < n; j++){
	    if(R[cur][j] == 1 && color[j] == 0){
		qu.push(j);
		color[j] = 1;
		ar[j] = ar[cur] + 1;
		flag = 1;
	    }
	}
	if(flag == 0){
	    qu.pop();
	}
    }
    return 0;
}

int main(void){
    int u, k, v;
    std::cin >> n;

    for(int i = 0; i < MAX; i++){
	for(int j = 0; j < MAX; j++){
	    R[i][j] = 0;
	}
    }

    for(int i = 0; i < MAX; i++){
	color[i] = 0;
    }

    for(int i = 0; i < n; i++){
	std::cin >> u >> k;
	u--;
	for(int j = 0; j < k; j++){
	    std::cin >> v;
	    v--;
	    R[u][v] = 1;
	}
    }

    search(0);

    for(int i = 0; i < n; i++){
	if(color[i] == 0){
	    std::cout << i + 1 << " " << -1 << std::endl;
	}else{
	    std::cout << i + 1 << " " << ar[i] << std::endl;
	}
    }

    return 0;
}