Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

var infty = 1000000000

type nodeKey int

type DirectedGraphNode struct {
	key       nodeKey
	adjacents []*DirectedGraphNode
	visited   bool
	d         int
	parent    *DirectedGraphNode
}

type DirectedGraph struct {
	nodes []DirectedGraphNode
}

func (g *DirectedGraph) setAdjacency(l, r nodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, &g.nodes[r])
}

func newDirectedGraph(nNodes int) *DirectedGraph {
	graph := DirectedGraph{}
	graph.nodes = make([]DirectedGraphNode, nNodes)
	for iNodes := 0; iNodes < nNodes; iNodes++ {
		graph.nodes[iNodes].key = nodeKey(iNodes)

		graph.nodes[iNodes].visited = false
		graph.nodes[iNodes].d = infty
		graph.nodes[iNodes].parent = nil
	}
	return &graph
}

type QueueEntry struct {
	node *DirectedGraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *DirectedGraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

func fillDistancesByBreadthFirstSearch(g *DirectedGraph, startNode *DirectedGraphNode) {
	startNode.visited = true
	startNode.d = 0

	queue := Queue{}
	queue.enqueue(startNode)
	for !queue.isEmpty() {
		u := queue.dequeue().node
		for _, adjacent := range u.adjacents {
			if !adjacent.visited {
				adjacent.visited = true
				adjacent.d = u.d + 1
				adjacent.parent = u
				queue.enqueue(adjacent)
			}
		}
		u.visited = true
	}
}

func main() {
	scanner.Split(bufio.ScanWords)

	nNodes := nextInt()
	g := newDirectedGraph(nNodes + 1)

	for iNode := 0; iNode < nNodes; iNode++ {
		nodeKeyLeft := nodeKey(nextInt())
		nAdj := nextInt()
		for iAdj := 0; iAdj < nAdj; iAdj++ {
			nodeKeyRight := nodeKey(nextInt())
			g.setAdjacency(nodeKeyLeft, nodeKeyRight)
		}
	}

	// fmt.Println(g)
	fillDistancesByBreadthFirstSearch(g, &g.nodes[1])
	// fmt.Println(g)

	for iNode := 1; iNode <= nNodes; iNode++ {
		node := g.nodes[iNode]
		distance := node.d
		if distance == infty {
			distance = -1
		}
		fmt.Println(node.key, distance)
	}
}

C++: #include <iostream>
#include <queue>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int n;
int color[MAX_N];
bool adj[MAX_N][MAX_N];
int d[MAX_N];

void bfs(int s) {
    queue<int> que; que.push(s);
    for (int i = 0; i < n; i++) d[i] = INF;
    d[s] = 0;

    int u;
    while (!que.empty()) {
        u = que.front(); que.pop();
        for (int v = 0; v < n; v++) {
            if (adj[u][v] && d[v] == INF) {
                d[v] = d[u] + 1;
                que.push(v);
            }
        }
    }
}
int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            adj[i][j] = false;
        }
    }

    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v; cin >> v;
            adj[u-1][v-1] = true;
        }
    }

    bfs(0);

    for (int i = 0; i < n; i++) {
        if (d[i] != INF) {
            cout << i+1 << " " << d[i] << endl;
        } else {
            cout << i+1 << " " << -1 << endl;
        }
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const ll mod = 1000000007;
const int inf = 1e9;
const long long INF = 1LL << 60;

vector<int> G[105];
int d[105];

int main()
{
    int n; cin >> n;
    for(int i = 0; i< n; i++){
        int u,k;
        cin >> u >> k;
        u--;
        for(int j = 0; j < k; j++){
            int x; cin >> x;
            x--;
            G[u].push_back(x);
        }
    }
    for(int i = 0; i < 105; i++){
        d[i] = -1;
    }
    queue<int> q;
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for(int i = 0; i < G[u].size(); i++){
            int nv = G[u][i];
            if(d[nv] == -1){
                d[nv] = d[u] + 1;
                q.push(nv);
            }
        }
    }
    for(int i = 0; i < n; i++){
        cout << i+1 << ' ' << d[i] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX = 101;
int n,m[MAX][MAX],d[MAX];
queue<int> s;

void bfs(int num)
{
	for(int i=1;i<=n;i++) d[i]=INT_MAX;
	s.push(num);
	d[num]=0;
	while(!s.empty())
	{
		int u=s.front();
		s.pop();
		for(int i=1;i<=n;i++)
		{
			if(m[u][i]==0) continue;
			if(d[i]!=INT_MAX) continue;
			s.push(i);
			d[i]=d[u]+1;
		}
	}
	for(int i=1;i<=n;i++) cout<<i<<" "<<( d[i]==INT_MAX ? (-1): d[i] )<<endl;
}
int main()
{
	int u,k,t;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>u>>k;
		for(int j=0;j<k;j++)
		{
			cin>>t;
			m[u][t]=1;
		}
	}
	bfs(1);
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int n;
bool g[110][110];
int d[110];

void bfs(int s)
{
	d[s] = 0;
	queue<int> q;
	q.push(s);

	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 1; i <= n; i++) {
			if (g[u][i] && d[i] == -1) {
				q.push(i);
				d[i] = d[u] + 1;
			}
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k, v;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v;
			g[u][v] = true;
		}
	}

	for (int i = 1; i <= n; i++) d[i] = -1;

	bfs(1);

	for (int i = 1; i <= n; i++)
		cout << i << " " << d[i] << endl;

	return 0;
}

C++: #include<stdio.h>
#include<queue>
using namespace std;
int tu[100][100];
int a[100];
int d[100];
int n;
void bfs(int x){
    queue<int> q;
    q.push(x);
    for(int i=0;i<n;i++)d[i]=10000000;
    d[x]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(tu[u][i]==0)continue;
            if(d[i]!=10000000)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
        }
        for(int i=0;i<n;i++){
            printf("%d ",i+1);
            if(d[i]==10000000)printf("-1");
            else printf("%d",d[i]);
            printf("\n");
        }
}
int main(){
    scanf("%d",&n);
    int v,m,k;
    for(int i=0;i<n;i++){
        scanf("%d%d",&v,&m);
        v--;
        for(int j=0;j<m;j++){
            scanf("%d",&k);
            k--;
            tu[v][k]=1;
        }
    }
    bfs(0);
    return 0;
}