Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const INFTY = (1 << 21)
const MAX = 100
const WHITE = 0
const GRAY = 1
const BLACK = 2

var time, n int

//隣接行列
var M [MAX][MAX]int

//d[i]: 頂点1から頂点iまでの距離
var color, d [MAX]int

func bfs(s int) {
	var q []int
	q = append(q, s)
	for i := 0; i < n; i++ {
		d[i] = INFTY
	}
	//sは開始点なので、0をいれる
	d[s] = 0

	//キューが空になるまで、以下を繰り返す
	for len(q) > 0 {
		u := q[0]
		q = q[1:]
		for v := 0; v < n; v++ {
			if M[u][v] == 0 {
				continue
			}
			if d[v] != INFTY {
				continue
			}
			d[v] = d[u] + 1
			q = append(q, v)
		}
	}

	for i := 0; i < n; i++ {
		fmt.Printf("%d ", i+1)
		if d[i] == INFTY {
			fmt.Printf("%d\n", -1)
		} else {
			fmt.Printf("%d\n", d[i])
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		u, k := nextInt(), nextInt()
		M[u-1][u-1] = 0

		//fmt.Println(u, k)
		if k > 0 {
			for j := 0; j < k; j++ {

				v := nextInt()
				M[u-1][v-1] = 1

			}
		}

	}

	bfs(0)

}

C++: #include <iostream>
#include <climits>
#include <vector>
#include <queue>
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF INT_MAX

using namespace std;

void bfs(int s, int n, int *color, int *d, vector<vector<int>> Adj){
    queue<int> Q;
    for (int u = s+1; u <= n; u++){
        color[u] = WHITE;
        d[u] = INF;
    }
    color[s] = GRAY;
    d[s] = 0;
    Q.push(s);
    while (Q.size() != 0){
        int zero = 0;
        int u = Q.front();
        Q.pop();
        for (int w = 1; w < Adj[u].size(); w++){
            int v = Adj[u][w];
            if (v == 0){
                break;
            }else if (color[v] == WHITE){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                Q.push(v);
                zero++;
            }
        }
        color[u] = BLACK;
    }
}

int main(int argc, char* argv[]) {
    int n, u, k, v;
    cin >> n;
    n++;
    int color[n], d[n], f[n];
    vector<vector<int>> Adj(n, vector<int>(n, 0));

    for (int i = 1; i < n; i++){
        cin >> u >> k;
        for (int j = 1; j <= k; j++){
            cin >> v;
            Adj[i][j] = v;
        }
    }

    bfs(1, n, color, d, Adj);

    for (int i = 1; i < n; i++){
        if (d[i] == INF)
            printf("%d -1\n", i);
        else
            printf("%d %d\n", i, d[i]);
    }
    return 0;
}

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

map<int, vector<int>> A;
int color[101];
int D[101];
int d = 0;

void dfs(int u) {
    queue<int> Q;
    Q.push(u);
    color[u] = 1;
    int current;
    D[u] = d;
    while(!Q.empty()) {
        current = Q.front();
        Q.pop();
        vector<int>::iterator it = A[current].begin();
        while(it != A[current].end()) {
            if (color[*it] == 0) {
                Q.push(*it);
                color[*it] = 1;
                D[*it] = D[current] + 1;
            }
            it++;
        }
    }
}

int main() {
    int n;
    cin >> n;

    int u, k;
    for (int i = 1; i <= n; i++) {
        color[i] = 0;
        D[i] = -1;
        cin >> u >> k;
        int ad;
        for (int j = 0; j < k; j++) {
            cin >> ad;
            A[u].push_back(ad);
        }
    }

    dfs(1);

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

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <utility>

using vvi = std::vector<std::vector<int>>;

std::vector<int> DistanseFrom(const vvi& edges, int from)
{
    using namespace std;
    constexpr int nil = -1;

    int nextDist {};
    const int n = edges.size();
    queue<int> currentQ;
    queue<int> nextQ;
    vector<int> distances(n, nil);

    nextQ.push(from);
    distances[from] = nextDist;
    ++nextDist;

    while (!nextQ.empty())
    {
        currentQ = nextQ;
        nextQ = queue<int>();
        
        while (!currentQ.empty())
        {
            int c = currentQ.back();
            currentQ.pop();

            for (const auto& e : edges[c])
            {
                if (distances[e] == nil)
                {
                    nextQ.push(e);
                    distances[e] = nextDist;
                }
            }
        }
        
        ++nextDist;
    }

    return distances;
}

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    //隣接リスト形式(1オリジン)
    vvi edges(n + 1);

    //読み込み
    for (int i = 1; i <= n; ++i)
    {
        int u, k;
        cin >> u >> k;

        edges[u] = vector<int>(k);

        for (int j = 0; j < k; ++j)
        {
            cin >> edges[u][j];
        }
    }

    auto dist = DistanseFrom(edges, 1);

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

int main()
{
    solve();
    return 0;
}

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

int main()
{
    int n;
    cin >> n;
    int adj[n][n];
    int V[n];
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u >> k;
        V[i] = k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            adj[u - 1][j] = v - 1;
        }
    }
    queue<int> q;
    int d[n];
    for (int i = 0; i < n; i++)
        d[i] = -1;
    d[0] = 0;
    q.push(0);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < V[u]; i++)
        {
            int v = adj[u][i];
            if (d[v] == -1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }

    for (int i = 0; i < n; i++)
        cout << i + 1 << ' ' << d[i] << endl;
}

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

class Vertex {
public:
    int num;
    bool flag;                  // 以前訪れたかどうか
    vector<int> adj;            // 隣接リスト
};

int dist[101];
void bfs(map<int, Vertex>& graph, Vertex s) {
    queue<Vertex> que;

    dist[s.num] = 0;
    graph[s.num].flag = true;
    que.push(s);
    while (!que.empty()) {
        Vertex u = que.front(); que.pop();
        for (auto i = 0; i < u.adj.size(); i++) {
            Vertex v = graph[u.adj[i]];
            if (!v.flag) {
                graph[u.adj[i]].flag = true;
                dist[v.num] = dist[u.num]+1;
                que.push(v);
            }
        }
    }
}

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < 101; i++) {
        dist[i] = -1;
    }

    map<int, Vertex> graph; // 頂点の番号->頂点
    for (int i = 0; i < n; i++) {
        Vertex v;
        int num, k;
        cin >> num >> k;
        v.flag = false;
        v.num = num;
        for (int j = 0; j < k; j++) {
            int tmp;
            cin >> tmp;
            v.adj.push_back(tmp);
        }
        graph[num] = v;
    }

    bfs(graph, graph[1]);
    for (int i = 1; i <= n; i++) {
        cout << i << " " << dist[i] << endl;
    }

    return 0;
}