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<bits/stdc++.h>
using namespace std;

static const int N = 100;
//static const int white = 0;
//static const int gray = 1;
//static const int black = 2;
static const int inf = (1 << 30);

int n, m[N][N];
int d[N];
//color[N];

void bfs(int s) {
	queue<int> q;
	for (int i = 0; i < n; i++) {
		//color[i] = white;
		d[i] = inf;
	}
	d[s] = 0;
	q.push(s);
	//color[s] = gray;
	while (!q.empty()) {
		for (int  i = 0; i < n; i++) {
			if (m[s][i] == 1 && d[i] == inf) {
				q.push(i);
				d[i] = d[s] + 1;
			}
		}
	//	color[s] = black;
		q.pop();
		s = q.front();
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			m[i][j] = 0;
		}
	}	
	int u, k, v;
	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);
	for (int i = 0; i < n; i++) {
		if (d[i] == inf)
			cout << i + 1 << " " << -1 << endl;
		else
			cout << i + 1 << " " << d[i] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define NODE_SIZE 101
#define NIL -1

using namespace std;

int adj[NODE_SIZE][NODE_SIZE];
int depth[NODE_SIZE];
int n, d;

void bfs(int node, int parent){
    depth[node] = depth[parent] + 1;
    for(int i = 1; i <= n; i++){
        if(adj[node][i] && (depth[i] == NIL || depth[i] > depth[node] + 1)){
            bfs(i, node);
        }
    }
}

int main(){
    int u, k, v;

    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> u >> k;
        depth[u] = NIL;
        while(k--){
            cin >> v;
            adj[u][v] = 1;
        }
    }


    depth[0] = NIL;
    bfs(1, 0);
    for(int i = 1; i <= n; i++){
        cout << i << " " << depth[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <stack>
#include <queue>
#include <list>
#include <stdio.h>
#include <algorithm>
#define MAX 100000
using namespace std;
typedef long long ll;
int map[255][255];
int d[255];
bool vis[255];
void bfs(int n,int pos,int distance)
{
	int a[255],j=0;
	vis[pos]=true;
	for(int i=1;i<=n;i++)
	{
		if(map[pos][i]&&!vis[i]){
			a[j++]=i;
			vis[i]=true;
			d[i]=distance;
		} 
	}
	for(int i=0;i<j;i++)
	{
		bfs(n,a[i],distance+1);
	}
}
int main()
{	
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			map[i][j]=0;
		}
	for(int i=0;i<n;i++)
	{
		int cur,k,u;
		cin>>cur>>k;
		while(k--)
		{
			cin>>u;
			map[cur][u]=1;
		}
	}
	for(int i=1;i<=n;i++)
	{
		vis[i]=false;
		d[i]=-1;
	}
	d[1]=0;
	bfs(n,1,1);
	for(int i=1;i<=n;i++)
	{
		cout<<i<<" "<<d[i]<<endl;
	}
	return 0;
}

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

int D[100];
int mat[100][100];

void bfs(int src);

int main() {
  int n;
  for (int i=0;i<100;++i) {
    for (int j=0;j<100;++j)
      mat[i][j] = 0;
    D[i] = -1;
  }
  cin >> n;
  for (int i=0;i<n;++i) {
    int buf1,buf2;
    cin >> buf1 >> buf2;
    for (int j=0;j<buf2;++j) {
      int buf3;
      cin >> buf3;
      mat[i][buf3-1] = 1;
    }
  }
  bfs(0);
  for (int i=0;i<n;++i)
    cout << i+1 << " " << D[i] << endl;
}

void bfs(int src) {
  // cout << "bfs root = " << src << endl;
  queue<int> Q; // 整数を管理するキューの定義
  Q.push(src);
  D[src] = 0; // 出発点
  while (!Q.empty()) {
    int cur = Q.front(); // 先頭要素を取り出す
    Q.pop();
    // 動作確認用表示
    // cout << "visiting " << cur << " " << D[cur] << endl;
    for (int dst=0;dst<100;++dst) { // 各行き先 dst に対して
      if (mat[cur][dst]==1&&D[dst]==-1) {
	// cur から dst に辺があり, dst が未訪問なら
	D[dst] = D[cur]+1;
	Q.push(dst); // dst を訪問先に加える
      }
    }
  }
}

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

using namespace std;
struct node
{
    node() {};
    node(int i, int n, int* xs) : id {i}, ns {vector<int>(xs, xs+n)}, d {-1} {};
    int id;
    vector<int> ns;
    int d;
};

void bfs(node* xs, int n, int st)
{
    queue<int> que {};
    bool used[n+1] {false};
    que.push(st);
    xs[st].d = 0;
    used[st] = true;
    while(que.size())
    {
        int p = que.front();
        que.pop();
        vector<int> ns = xs[p].ns;
        for(auto x : ns)
        {
            if(used[x])
            {
                continue;
            }
            xs[x].d = xs[p].d + 1;
            used[x] = true;
            que.push(x);
        }
    }
}

int main()
{
    int n;
    cin >> n;
    node graph[n+1];
    for(int i = 0; i < n; ++i)
    {
        int id;
        int m;
        cin >> id >> m;
        int xs[m];
        for(int j = 0; j < m; ++j)
        {
            cin >> xs[j];
        }
        graph[id] = node(id, m, xs);
    }
    bfs(graph, n, 1);
    for(int i = 1; i <= n; ++i)
    {
        cout << i << ' ' << graph[i].d << endl;
    }
}