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