Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const (
	N     = 100
	INFTY = 1 << 21
)

type Graph struct {
	M [N][N]int
	d [N]int
}

func (g *Graph) bfs(s, n int) string {
	var q []int
	q = append(q, s) // push

	for i := 0; i < n; i++ {
		g.d[i] = INFTY
	}
	g.d[s] = 0

	for len(q) != 0 {
		u := q[0]
		q = q[1:] // pop
		for v := 0; v < n; v++ {
			if g.M[u][v] == 0 || g.d[v] != INFTY {
				continue
			}
			g.d[v] = g.d[u] + 1
			q = append(q, v) // push
		}
	}

	var out bytes.Buffer
	for i := 0; i < n; i++ {
		if g.d[i] == INFTY {
			out.WriteString(fmt.Sprintf("%d -1\n", i+1))
			continue
		}
		out.WriteString(fmt.Sprintf("%d %d\n", i+1, g.d[i]))
	}

	return strings.TrimRight(out.String(), "\n")
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		sc.Scan()
		u, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		k, _ := strconv.Atoi(sc.Text())
		u--
		for j := 0; j < k; j++ {
			sc.Scan()
			v, _ := strconv.Atoi(sc.Text())
			v--
			g.M[u][v] = 1
		}
	}

	return g.bfs(0, n)
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <iostream>
#include <ios>
#include <vector>
#include <utility>

using namespace std;

void read_data(int n, vector<vector<int>> & Es){
    int u = 0, k = 0, v = 0;
    while (n--){
        cin >> u;
        cin >> k;
        while (k--){
            cin >> v;
            Es[u - 1].push_back(v - 1);
        }
    }
}

void wfs(int node, vector<int> & dist, vector<vector<int>> & Es){
    int d = 0;
    vector<int> frontiers;
    frontiers.push_back(node);
    while (!frontiers.empty()){
        vector<int> new_frontiers;
        for (auto u : frontiers){
            if (dist[u] == -1){
                dist[u] = d;
                for (auto v : Es[u]){
                    new_frontiers.push_back(v);
                }
            }
        }
        frontiers = move(new_frontiers);
        ++d;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int n = 0;
    cin >> n;
    vector< vector<int> > Es(n);
    vector<int> dist(n, -1);
    read_data(n, Es);
    wfs(0, dist, Es);
    for (int i = 0; i != n; ++i) cout << i + 1 << ' ' << dist[i] << endl;
    
    return 0;
}

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

#define N 100
#define MAX 100000

int A[N + 1][N + 1] = {};
int n, u, k, v;
int t1[N + 1]; //発見時刻

//sは始点,tは時間
void bfs(int s, int t) {
    t1[s] = t;
    for(int i = 1; i <= n; ++i) {
        if(A[s][i] == 1 && t < t1[i]) {
            bfs(i, t + 1);
        }
    }
}

int main(void) {
    cin >> n;
    
    for(int i = 0; i < n; ++i) {
        cin >> u >> k;
        for(int j = 0; j < k; ++j) {
            cin >> v;
            A[u][v] = 1;
        }
    }
    
    for(int i = 0; i <= n; ++i) t1[i] = MAX;
    
    bfs(1, 0);
    
    for(int i = 1; i <= n; ++i) {
        cout << i << ' ';
        if(t1[i] == MAX) cout << -1 << endl;
        else cout << t1[i] << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, u, k, vi;
    std::cin >> n;
    std::vector<std::vector<int>> v(n+1);
    std::queue<int> ex;
    ex.push(1);
    int distance[101]={};
    for (int i=2; i<101; i++) {
        distance[i] = -1;
    }
    for (int i=0; i<n; i++) {
        std::cin >> u >> k;
        for (int j=0; j<k; j++) {
            std::cin >> vi;
            v[u].push_back(vi);
        }
    }

    while (not ex.empty()) {
        int a = ex.front();
        ex.pop();
        for (int i=0; i<v[a].size(); i++) {
            if (distance[v[a][i]] == -1) {
                ex.push(v[a][i]);
                distance[v[a][i]] = distance[a] + 1;
            }
        }
    }

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

    return 0;
}

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

const int Max = 100;
int map[Max + 1][Max + 1];

typedef struct{
	bool startflag;
	int distance;
}State;

int main()
{
	int n, k, l, temp;
	std::queue<int> q;
	std::vector<State> state;

	for (int i = 1; i < Max + 1; i++){
		for (int j = 1; j < Max + 1; j++){
			map[i][j] = 0;
		}
	}

	std::cin >> n;
	for (int i = 1; i <= n; i++){
		std::cin >> k >> l;
		for (int j = 1; j <= l; j++){
			std::cin >> temp;
			map[k][temp] = 1;
		}
	}

	State s = { false, 0 };
	for (int i = 0; i <= n; i++){
		state.push_back(s);
	}

	int d = 0;
	while (1){
		if (q.empty() == true){
			bool f = false;
			for (int i = 1; i <= n; i++){
				if (state[i].startflag == false){
					if (i != 1){
						state[i].startflag = true;
						state[i].distance = -1;
						continue;
					}
					q.push(i);
					state[i].startflag = true;
					d = 0;
					state[i].distance = d;
					d++;
					f = true;
					break;
				}
			}
			if (f == false){
				break;
			}
		}
		int size = q.size();
		for (int j = 0; j < size; j++){
			for (int i = 1; i <= n; i++){
				if (map[q.front()][i] == 1 && state[i].startflag == false){
					q.push(i);
					state[i].startflag = true;
					state[i].distance = d;
				}
			}
			q.pop();
		}
		d++;
	}

	for (int i = 1; i <= n; i++){
		std::cout << i << " " << state[i].distance << std::endl;
	}

	return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];   //頂点からの距離を格納するための配列

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for ( int i = 0; i < n; i++ ) d[i] = INFTY;
    d[s] = 0;
    int u;
    while ( !q.empty() ) {
        u = q.front(); q.pop();   //front:キューから取り出し、pop:キューから削除
        for ( int v = 0; v < n; v++ ) {
            if ( M[u][v] == 0 ) continue;   //頂点同士が隣接しているか確認
            if ( d[v] != INFTY ) continue;   //頂点を訪れたか確認
            d[v] = d[u] + 1;   //始点からの距離を格納
            q.push(v);   //次に調査する頂点をキューに追加
        }
    }
    for ( int i = 0; i < n; i++ ) {
        cout << i+1 << " " << ( (d[i] == INFTY) ? (-1) : d[i] ) << endl;
    }
}

int main() {
    int u, k, v;   //u:頂点番号、k:uの出次数、v:隣接する頂点番号
    
    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);
    
    return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int m[100][100];
int d[100];
int n;
const int INF = 1e8;

void bfs(int u)
{
    queue<int> q;
    q.push(u);

    d[u] = 0;

    while (!q.empty())
    {
        int t = q.front();
        q.pop();

        rep(i, n)
        {
            if (m[t][i] == 0 || d[i] != INF)
                continue;
            d[i] = d[t] + 1;
            q.push(i);
        }
    }
}

int main()
{
    cin >> n;
    rep(i, n) d[i] = INF;

    rep(i, n) rep(j, n) m[i][j] = 0;

    rep(i, n)
    {
        int u, k;
        cin >> u >> k;
        --u;
        rep(j, k)
        {
            int v;
            cin >> v;
            m[u][--v] = 1;
        }
    }

    bfs(0);

    rep(i, n)
    {
        if (d[i] == INF)
            cout << i + 1 << " " << -1 << endl;
        else
            cout << i + 1 << " " << d[i] << endl;
    }

    return 0;
}