Go: package main

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

const MAX = 1000
const NIL = -1

type Node struct {
	id    int
	depth int
}

type Queue struct {
	head      int
	tail      int
	container [MAX]Node
}

func (q *Queue) inQueue(x Node) {
	q.container[q.tail] = x
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) deQueue() Node {
	x := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return x
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()
	route := make([][]int, n)
	for i := range route {
		u, k := scanInt(), scanInt()
		u--
		route[u] = make([]int, k)
		for j := range route[u] {
			num := scanInt()
			num--
			route[i][j] = num
		}
	}

	d := make([]int, n)
	for i := range d {
		d[i] = NIL
	}

	start := Node{0, 0}
	que := Queue{}
	que.inQueue(start)
	for !que.isEmpty() {
		node := que.deQueue()
		if d[node.id] != NIL {
			continue
		}
		d[node.id] = node.depth

		for _, nextId := range route[node.id] {
			next := Node{id: nextId, depth: node.depth + 1}
			que.inQueue(next)
		}
	}

	for i, depth := range d {
		fmt.Println(i+1, depth)
	}
}

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

using namespace std;

int D[110]; // ?????¢????¨???¶
int v, A[100][100], u, k, w;
queue<int> Q;



int main(){
	cin >> v;
	// ???????????§??????v
	for (int i = 0; i < v; ++i) {

		cin >> u >> k;
		// ??????????????????u???k?????\?????????????????°??????

		for (int j = 0; j < k; ++j) {

			cin >> w;
			A[u - 1][w - 1] = 1;

			// ??£??\???????????§?¨??????????????????????????????????

		}
	}
		Q.push(0);
		D[0] = 0;

		while (!Q.empty()) {
			int cur = Q.front();
			Q.pop();
			for (int i = 0; i < v; ++i) {
				if (A[cur][i] == 1 && D[i] == 0) {
					D[i] = D[cur] + 1;
					Q.push(i);
				}
			}
		}

		for (int l = 0; l < v; ++l) {
			//????????????????????????????????????
			if (D[l] == 0) { D[l] = -1; }
			if (l == 0) { D[l] = 0; }
			cout << l + 1 << " " << D[l] << endl;
		}

	}

C++: #include <iostream>
#include <queue>
using namespace std;
int D[110]={};
int R[110][110]={};
int n,a,b,k;
void bfs(int src){
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	while (! Q.empty()) {
		int cur = Q.front();
		Q.pop();
		for(int dst=1;dst<n+1;dst++){
			if(R[cur][dst]==1 && D[dst]==0){
				D[dst] =D[cur]+1;
				Q.push(dst);
			}
		}
	}
}
int main() {
	cin >> n;
	for(int i=0;i<n;i++){
		cin>>a;
		cin>>k;
		for(int j=0;j<k;j++){
		cin>>b;
		R[a][b]=1;
		}
	}
	bfs(1);
	cout <<"1 0"<<endl;
	for(int m=2;m<n+1;m++){
		if(D[m]==0){
		cout <<m<<" "<<-1<<endl;
		}else{
		cout <<m<<" "<<D[m]<<endl;
	}
	}
	return 0;
}

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