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>
#include<string>
#define NIL -1
using namespace std;
string color[101];//白色未访问，灰色访问，黑色退出队列
queue<int >BreathFirstSearch;//创建队列
int Distance[101];
int AM[101][101];
int total;
void includeInsertQueue(int i)
{
	for (int j = 0; j <= total; j++)
	{
		if (AM[i][j] && color[j] == "White")//存在且未被访问
		{
			color[j] = "Grey", Distance[j] =Distance[i]+1;//找到的点距离是其上一个再加一
			BreathFirstSearch.push(j);
		}
	}
}
void includeFirstSearch(int i)
{
	int next;
	BreathFirstSearch.push(i);//压入起点
	Distance[i] =0;//起点到起点的距离为0；
	color[i] = "Grey";
	while (!BreathFirstSearch.empty())
	{
		next = BreathFirstSearch.front();//队列头中取出元素
		BreathFirstSearch.pop();
		color[next] = "Black";
		includeInsertQueue(next);

	}
}
int main()
{	int TotalOfConnectedNode, node[101], connectednode;
	for (int i = 0; i < 101; i++) Distance[i]= NIL;
	for (int i = 0; i < 101; i++)color[i] = "White";
	for (int i = 0; i < 101; i++)
		for (int j = 0; j < 101; j++)
			AM[i][j] = 0;
	cin >> total;
	for (int i = 0; i < total; i++)//有向图
	{
		cin >> node[i] >> TotalOfConnectedNode;
		if (TotalOfConnectedNode == 0) continue;
		for (int j = 0; j < TotalOfConnectedNode; j++)
		{
			cin >> connectednode;
			AM[node[i]][connectednode] = 1;
		}
	}
	includeFirstSearch(node[0]);
	for (int i = 0; i < total; i++)
		cout << node[i] << " " << Distance[node[i]] << endl;

	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;

int n;
Graph G;

int main()
{
    cin >> n;
    G.resize(n);
    rep(i, n)
    {
        int id, k;
        cin >> id >> k;
        rep(j, k)
        {
            int v;
            cin >> v;
            G[i].push_back(v - 1);
        }
    }
    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    que.push(0);
    while (!que.empty())
    {
        int v = que.front();
        que.pop();
        for (int nv : G[v])
        {
            if (dist[nv] != -1)
                continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i, n)
    {
        cout << i + 1 << " " << dist[i] << endl;
    }
    return 0;
}

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

const int nIMPOS = -1;
struct StDir
{
  int m_nx;
  int m_nEtc;

  StDir(int nx) : m_nx(nx), m_nEtc(0)
  { }
};

void fnInput(vector<vector<int>>& rvvnAdjcy, vector<int>& rvnDst)
{
  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;
    }
  }
  rvnDst.resize(nxSiz + 1, nIMPOS);
}

void fnBreadthFirstSearch(const vector<vector<int>>& cnrvvnAdjcy, 
                          queue<StDir>& rqoBFSrch, vector<int>& rvnDst)
{
  while (rqoBFSrch.size())
  {
    StDir oDir = rqoBFSrch.front(); rqoBFSrch.pop();
    if (rvnDst[oDir.m_nx] == nIMPOS)  continue;

    for (int ny = 1; ny < cnrvvnAdjcy[oDir.m_nx].size(); ++ny)
      if (cnrvvnAdjcy[oDir.m_nx][ny] == 1)
      {
        int nDst = rvnDst[oDir.m_nx] + 1;
        if (rvnDst[ny] == nIMPOS)
          rvnDst[ny] = nDst;
        else
          rvnDst[ny] = min(rvnDst[ny], nDst);

        StDir oDir(ny);
        rqoBFSrch.push(oDir);
      }
  }
}

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

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<vector<int>> vvnAdjcy;
  queue<StDir> qoBFSrch;
  vector<int>  vnDst;

  fnInput(vvnAdjcy, vnDst);

  vnDst[1] = 0;
  StDir oDir(1);
  qoBFSrch.push(oDir);
  fnBreadthFirstSearch(vvnAdjcy, qoBFSrch, vnDst);
  fnResult(vnDst);

  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;

static const int INFTY = INT_MAX;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
int M[110][110] = {};
int color[110];
int d[110];
queue<int> Q;

void bfs(int s) {
  rep(u, n) color[u] = WHITE;
  rep(u, n) d[u] = INFTY;

  color[s] = GRAY;
  d[s] = 0;
  Q.push(s);

  while (!Q.empty()) {
    int u = Q.front(); Q.pop();
    rep(v, n) {
      if (M[u][v] && color[v] == WHITE) {
        color[v] = GRAY;
        d[v] = d[u] + 1;
        Q.push(v);
      }
    }
    color[u] = BLACK;
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    int id, deg;
    cin >> id >> deg;
    --id;    // 0-indexedに直す
    rep(j, deg) {
      int v;
      cin >> v;
      --v;    // 0-indexedに直す
      ++M[id][v];
    }
  }
  bfs(0);
  rep(i, n) {
    cout << i + 1 << " ";
    if (d[i] != INFTY) cout << d[i];
    else cout << -1;
    cout << endl;
  }
  return 0;
}