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<vector>
#include<queue>
using namespace std;

void calc(const vector<vector<int> >& G, vector<int>& D,
                                        queue<int>& Q, int dim) {
    if (Q.empty()) return;
    queue<int> Q2;
    while (!Q.empty()) {
        int i = Q.front();
        for (int j = 1; j < G[i].size(); j++) {
            if (G[i][j] && D[j] == -1) {
                Q2.push(j);
                D[j] = dim;
            }
        }
        Q.pop();
    }
    calc(G, D, Q2, dim + 1);
}

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

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << G[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    vector<int> D(n + 1, -1);

    queue<int> Q;
    Q.push(1);
    D[1] = 0;
    calc(G, D, Q, 1);

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

    return 0;
}

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

using namespace std;

int n;
int u[102], k[102], t[102][102];
queue<int>Q;
vector<int>V;
int color[102];
int d[102];

void bfs(int s){
  for(int i = 1; i <= n; ++i){
    if(u[i] != s){
      color[u[i]] = 0;
      d[u[i]] = 150;
    }
  }
  d[s] = 0;
  int pi = 0;
  Q.push(s);
  while(!Q.empty()){
    int m = Q.front();
    Q.pop();
    for(int i = 1; i <= k[m]; ++i){
      if(color[t[m][i]] == 0){
        color[t[m][i]] = 2;
        d[t[m][i]] = d[m] + 1;
        pi = t[m][i];
        Q.push(t[m][i]);
      }
    }
    color[m] = 1;
  }
  for(int i = 1; i <= n; ++i){
    if(d[i] == 150){
      cout << i << " " << -1 << endl;
    }else{
      cout << i << " " << d[i] << endl;
    }
  }
  
}

int main(){
  cin >> n;
  for(int i = 1; i <= n; ++i){
    cin >> u[i] >> k[i];
    for(int j = 1; j <= k[i]; ++j){
      cin >> t[i][j];
    }
  }
  bfs(1);
}

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

	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++: #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();
        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;
    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++: #include <iostream>
#include <queue>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int adj_matrix[101][101] = {0};
    int visit[101] = {0};
    queue<int> q;
    int dist_from_start[101] = {0};
    for (int i = 1; i <= n; i++)
    {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++)
        {
            int x;
            cin >> x;
            adj_matrix[u][x] = 1;
        }
    }

    q.push(1);
    visit[1] = 1;
    while (!q.empty())
    {
        int now = q.front();
        for (int i = 1; i <= n; i++)
        {
            if (adj_matrix[now][i] == 1 && visit[i] == 0)
            {
                visit[i] = 1;
                q.push(i);
                dist_from_start[i] = dist_from_start[now] + 1;
            }
        }
        q.pop();
    }

    for (int i = 1; i <= n; i++)
    {
        if (i == 1 || dist_from_start[i] > 0)
        {
            cout << i << " " << dist_from_start[i] << endl;
        }
        else
        {
            cout << i << " " << -1 << endl;
        }
    }

    return 0;
}