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>
using namespace std;

const int MAX = 100;
int n;
int G[MAX + 1][MAX + 1];
int states[MAX + 1];
int d[MAX + 1];
queue<int> Q;

void bfs(int id) {
  d[id] = 0;
  Q.push(id);

  while (!Q.empty()) {
    id = Q.front(); Q.pop();
    for (int i = 0; i < n; i++) {
      if (G[id][i] == 1 && states[i] == 0) {
        Q.push(i);
        states[i] = 1;
        d[i] = d[id] + 1;
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;

  int u, v, k;

  for (int i = 0; i < n; i++) {
    d[i] = -1;
    states[i] = 0;
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }

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

  bfs(0);

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

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <queue>

using namespace std;

const int WHITE = 0, GRAY = 1, BLACK = 2;

struct BFS {
    int n;
    vector<vector<int>> g;
    vector<int> depth;
    vector<int> state;
    int time = 0;

    BFS(int n) : n(n) {
        g.assign(n, vector<int>());
        depth.assign(n, -1);
        state.assign(n, WHITE);
    }

    void addEdge(int u, int v) {
        g[u].push_back(v);
    }

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

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            if (state[u] == BLACK) {
                return;
            }
            for (int v : g[u]) {
                if (state[v] == WHITE) {
                    state[v] = GRAY;
                    depth[v] = depth[u] + 1;
                    q.push(v);
                }
            }
            state[u] = BLACK;
        }
    }

    void print() {
        for (int i = 0; i < n; i++) {
            cout << i << ":";
            for (int v : g[i]) {
                cout << v << " ";
            }
            cout << endl;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    BFS dfs(n);
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            int v;
            cin >> v;
            v--;
            dfs.addEdge(u, v);
        }
    }

    //dfs.print();

    dfs.bfs(0);

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

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> d;
	std::list<std::vector<int> > Q;
	int n;

	Graph(int n);

	void BreadthFirstSearch();

};

Graph::Graph(int n){
	A.resize(n);
	d.resize(n);
	for (int i = 0; i < n; i++){
		A[i].resize(n);
		for (int j = 0; j < n; j++){
			A[i][j] = 0;
		}
		d[i] = -1;
	}
	this->n = n;
}

void Graph::BreadthFirstSearch(){
	d[0] = 0;
	std::vector<int> a(2, 0);
	a[1] = 1;
	Q.push_back(a);
	while (!Q.empty()){
		std::vector<int> f = Q.front();
		Q.pop_front();
		for (int i = 0; i < n; i++){
			if (A[f[0]][i] == 1 && d[i] == -1){
				d[i] = f[1];
				a[0] = i;
				a[1] = f[1] + 1;
				Q.push_back(a);
			}
		}
	}
	return;
}

int main(){
	int n, u, k, v;
	scanf("%d", &n);
	Graph G(n);
	for (int i = 0; i < n; i++){
		scanf("%d%d", &u, &k);
		for (int j = 0; j < k; j++){
			scanf("%d", &v);
			G.A[u - 1][v - 1] = 1;
		}
	}

	G.BreadthFirstSearch();

	for (int i = 0; i < n; i++){
		printf("%d %d\n", i+1, G.d[i]);
	}


	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<utility>

using namespace std;



int main() {
	queue<pair<int, int>> q;
	int n,row,size,tmp;
	pair<int, int> ptmp;
	cin >> n;
	int** mat = new int* [n];
	int *distance = new int[n];
	for (int i = 0;i < n;i++) {
		cin >> row >> size;
		mat[row - 1] = new int[n];
		for (int j = 0;j < n;j++) {
			mat[row - 1][j] = 0;
		}
		for (int j = 0;j < size;j++) {
			cin >> tmp;
			mat[row - 1][tmp - 1] = 1;
		}
		distance[i] = -1;
	}
	distance[0] = 0;
	q.push(make_pair(0, 0));
	while (!q.empty()) {
		ptmp = q.front();
		q.pop();
		for (int i = 0;i < n;i++) {
			if (mat[ptmp.first][i] && distance[i] == -1) {
				distance[i] = ptmp.second + 1;
				q.push(make_pair(i, ptmp.second + 1));
			}
		}
	}
	for (int i = 0;i < n;i++) {
		cout << i + 1 << " " << distance[i] << endl;
	}


}