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;

const int nIMPOS = -1;
struct StDist
{
  int m_nDist;
  int m_nDone;

  StDist(int nDist) : m_nDist(nDist), m_nDone(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<StDist>& rvoDist)
{
  int nxSiz;
  cin >> nxSiz;

  rvvnAdjcy.resize(nxSiz + 1, vector<int>(nxSiz + 1, 0));
  for (int i = 0; i < nxSiz; ++i)
  {
    int nx, nySiz;
    cin >> nx >> nySiz;
    for (int j = 0; j < nySiz; ++j)
    {
      int ny;
      cin >> ny;
      rvvnAdjcy[nx][ny] = 1;
    }
  }
  StDist oDist(nIMPOS);
  rvoDist.resize(nxSiz + 1, oDist);
}

void fnBreadthFirstSearch(vector<vector<int>>& rvvnAdjcy, 
                          queue<int>& rqnBFSrch, vector<StDist>& rvoDist)
{
  while (rqnBFSrch.size())
  {
    int nx = rqnBFSrch.front(); rqnBFSrch.pop();
    if (rvoDist[nx].m_nDist == nIMPOS)  continue;

    for (int ny = 1; ny < rvvnAdjcy[nx].size(); ++ny)
      if (rvvnAdjcy[nx][ny] == 1    &&
          rvoDist[nx].m_nDone == 0)
      {
        rvoDist[nx].m_nDone = 1;
        int nDist = rvoDist[nx].m_nDist + 1;
        if (rvoDist[ny].m_nDist == nIMPOS)
          rvoDist[ny].m_nDist = nDist;
        else
          rvoDist[ny].m_nDist = min(rvoDist[ny].m_nDist, nDist);

        rqnBFSrch.push(ny);
      }
  }
}

void fnResult(const vector<StDist>& cnrvoDist)
{
  for (int i = 1; i < cnrvoDist.size(); ++i)
    cout << i <<  " " << cnrvoDist[i].m_nDist << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<int>  qnBFSrch;
  vector<StDist> voDist;

  fnInput(vvnAdjcy, voDist);

  voDist[1].m_nDist = 0;
  qnBFSrch.push(1);
  fnBreadthFirstSearch(vvnAdjcy, qnBFSrch, voDist);
  fnResult(voDist);

  return 0;
}

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

#define NODE_NUM 100

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int id);
	int dist;
};

Node::Node()
{
	next_num = 0;
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node Node_list[NODE_NUM + 1];

bool Node::Search(int id)
{
	for (int k = 0; k < next_num; k++) {
		if (Node_list[next_node_list[k]].dist == -1) {
			Node_list[next_node_list[k]].dist = dist + 1;
		}
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == id) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		que.push(next_node_list[j]);
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u;
		cin >> k;
		Node_list[u] = Node();
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			Node_list[u].AddNextNode(v);
		}
	}
	
	//dist
	for (int k = 1; k <= n; k++) {
		Node_list[k].dist = -1;
	}

	Node_list[1].dist = 0;
	for (int i = 2; i <= n; i++) {
		bool ret = Node_list[1].Search(i);
		if (ret) continue;
		if (que.empty()) continue;
		do {
			ret = Node_list[que.front()].Search(i);
			que.pop();
		} while (!ret);
		while (!que.empty()) {
			que.pop();
		}
	}

	//output
	for (int i = 1; i <= n; i++) {
		cout << i << " " << Node_list[i].dist << "\n";
	}
	
	return 0;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#define INF (int)1e9 + 7
using namespace std;
int n;
vector<pair<int, int> > adj[10000];
void dijkstra();
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int u, k;
        cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v; //vertex cost
            cin >> v;
            adj[u].push_back(make_pair(1, v));
        }
    }
    dijkstra();
    return 0;
}
void dijkstra() {
    int dist[n + 1];
    dist[1] = 0;
    for (int i = 2; i <= n; i++) {
        dist[i] = INF;
    }
    priority_queue<pair<int, int> > q;
    q.push(make_pair(0, 1));
    while (!q.empty()) {
        int u = q.top().second;
        //int co = -q.top().first;
        q.pop();
        for (int i = 0; i < adj[u].size(); i++) {
            int v = adj[u][i].second; //adj[u]のi番目の番号
            int co2 = adj[u][i].first;
            if (dist[u] + co2 < dist[v]) {
                dist[v] = co2 + dist[u];
                q.push(make_pair(-dist[v] ,v));
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << i << " ";
        if (dist[i] == (int)1e9 + 7) {
            cout << -1 << endl;
        } else {
            cout << dist[i] << endl;
        }
    }
}