Go: package main

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

type Pair struct {
	v int
	d int
}

type Queue []Pair

func (q *Queue) push(v Pair) {
	*q = append(*q, v)
}

func (q *Queue) pop() Pair {
	v := (*q)[0]
	*q = (*q)[1:]
	return v
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nVerts := scanInt(sc)
	graph := make([][]int, nVerts+1)

	for i := 0; i < nVerts; i++ {
		u := scanInt(sc)
		nDegrees := scanInt(sc)

		graph[u] = make([]int, nDegrees)
		for d := 0; d < nDegrees; d++ {
			graph[u][d] = scanInt(sc)
		}
	}

	paths := make([]int, nVerts+1)

	queue := Queue(make([]Pair, 0))
	queue.push(Pair{1, 1})
	paths[1] = 1

	for len(queue) > 0 {
		p := queue.pop()
		d := p.d + 1
		for _, v := range graph[p.v] {
			if paths[v] == 0 {
				paths[v] = d
				queue.push(Pair{v, d})
			}
		}
	}

	w := bufio.NewWriter(os.Stdout)
	for i := 1; i <= nVerts; i++ {
		fmt.Fprintf(w, "%d %d\n", i, paths[i]-1)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

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

int n;
vector<int> G[110];
int d[110];

void bfs(){
  queue<int> que;
  for(int i=0; i<n; i++){
    d[i] = -1;
  }
  d[0] = 0;
  que.push(0);
  while(!que.empty()){
    int v = que.front(); que.pop();
    for(int i=0; i<G[v].size(); i++){
      int nv = G[v][i];
      if(d[nv] < 0){
        que.push(nv);
        d[nv] = d[v] + 1;
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0; i<n; i++){
    int u, k; cin >> u >> k;
    for(int i=0; i<k; i++){
      int c; cin >> c;
      G[u-1].push_back(c-1);
    }
  }
  bfs();
  for(int i=0; i<n; i++){
    cout << i+1 << " " << d[i] << endl;
  }
}