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 <queue>
#include <string>

using namespace std;

const int MAX = 100;

// 幅優先探索
// 最短距離を求める。

enum ColorStatus
{
    WHITE_no_visit,
    GRAY_inqueue,
    BLACK_finished
};



struct Node
{
    // 頂点 0オリジン
    int id;
    // 頂点1からの距離。たどり着けない場合は-1
    int direction;
};

int main(int argc, char *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    int M[MAX][MAX] = { 0 };
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u; // 頂点番号
        cin >> k; // 次数
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            M[u-1][j] = 1;
        }
    }

    Node nodes[MAX] = { 0, 0 };
    for (int i = 0; i < n; i++)
    {
        nodes[i].id = i;
        nodes[i].direction = -1;
    }
    // Node0は、始点sで、入力データにはないが、ここからプログラムを始めるようにする。
    Node startNode = nodes[0];
    startNode.direction = 0;

    queue<Node> q;
    q.push(startNode);

    while (!q.empty())
    {
        // c++のqueueはpopで値を返さない。
        // 先頭の参照を取得後、popで削除。
        Node uNode = q.front();
        q.pop();
        // ここで訪問完了

        for (int v = 0; v < n; v++)
        {
            if (M[uNode.id][v] == 0) continue;     // 行き先がない。
            if (nodes[v].direction == -1) continue;   // 到達状態ではない。

            // u->vで、頂点uが保持している距離+1をnodes[v]に入れる
            nodes[v].direction = nodes[uNode.id].direction + 1;

            // for文なので、頂点uに隣接するvがすべてここでキューに入る
            q.push(nodes[v]);
        }
    }

    // 出力
    for (int i = 0; i < n; i++)
    {
        Node node = nodes[i];
        cout << node.id << " " << node.direction << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    cout << fixed << setprecision(10);
    
    int n; cin >> n;
    vector<vector<int>> e(n);
    rep(i, n) {
        int id; cin >> id; id--;
        int ne; cin >> ne;
        rep(j, ne) {
            int tn; cin >> tn; tn--;
            e[id].emplace_back(tn);
        }
    }

    queue<int> q;
    vector<int> aa(n);
    vector<int> d(n);
    q.emplace(0);
    aa[0] = 1;

    while(!q.empty()) {
        int nn = q.front();
        q.pop();
        rep(i, e[nn].size()) {
            if (aa[e[nn][i]]) continue;
            q.emplace(e[nn][i]);
            d[e[nn][i]] = d[nn] + 1;
            aa[e[nn][i]] = 1;
        }
    }
    rep(i, n) {
        if (aa[i]) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
        
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (1E9+7)
#define PI 3.1415926535897932384626433832795

std::ostream &operator<<(std::ostream &out, const vector<int> &tgt)
{
	string s;
	for (int i = 0; i<tgt.size(); i++) {
		s += (to_string(tgt[i]) + ((i != tgt.size() - 1) ? " " : ""));
	}
	out << s;
	return out;
}
struct node {
	vi to;
	int dist = -1;
	bool visited = false;
};

vector<node> g;
int t = 0;

void bfs(int n) {
	queue<pii> q;
	q.push({n,0});
	while (!q.empty()) {
		int dis = q.front().sc;
		int node = q.front().fs;
		q.pop();
		if (g[node].visited)continue;
		//cout << node + 1 << endl;
		g[node].dist = dis;
		g[node].visited = true;
		rep(i, g[node].to.size()) {
			if (!g[g[node].to[i]].visited)q.push({ g[node].to[i],dis + 1 });
		}
	}
}

int main(){
	int n,in,inn;
	cin >> n;
	g.resize(n);
	rep(i, n) {
		cin >> in;
		cin >> in;
		rep(j, in) {
			cin >> inn;
			g[i].to.push_back(inn-1);
		}
	}
	rep(i, n) {
		if (!g[i].visited) {
			bfs(0);
		}
	}
	rep(i, n) {
		cout <<i+1<<" "<< g[i].dist <<endl;
	}
}

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

struct node
{
	int distance;
	bool accessed;
	node(int d, bool a) : distance(d), accessed(a) {}
};

void bfs(vector<node> &nodes, vector<vector<bool>> &conn_mat)
{ //index = n-1
	nodes[0].accessed = true;
	nodes[0].distance = 0;
	queue<int> bfs_queue;
	bfs_queue.push(0);
	int dis = 0;
	while (!bfs_queue.empty())
	{
		int index = bfs_queue.front();
		int i = 0;
		bfs_queue.pop();
		for (auto connected : conn_mat[index])
		{
			if (connected && (!nodes[i].accessed))
			{
				bfs_queue.push(i);
				nodes[i].accessed = true;
				nodes[i].distance = nodes[index].distance + 1;
			}
			i++;
		}
	}
}

int main()
{

	int n_node, index, n_cn, cn;
	cin >> n_node;
	vector<node> nodes(n_node, node(-1, false));
	vector<bool> vertex(n_node, false);
	vector<vector<bool>> conn_mat(n_node, vertex);
	while (n_node--)
	{
		cin >> index >> n_cn;
		while (n_cn--)
		{
			cin >> cn;
			conn_mat[index - 1][cn - 1] = true;
		}
	}
	bfs(nodes, conn_mat);
	int i = 0;
	for (node nd : nodes)
	{
		i++;
		cout << i << ' ' << nd.distance << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
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 = 0; dst < n; dst++ ) {
			if( D[dst] == 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}
int main() {
	cin >> n;
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			a[i][j] = 0;
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
		bfs(0);
}