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;
static const int N = 100;
static const int INFTY = -1;

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 <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
static const int MAX = 100;
int N;
int ADJ[MAX][MAX];
int depth[MAX] = {0};

enum STATUS { ACTIVE = 0, MARKED, VISITED, };
int status[MAX] = {ACTIVE};

void visit(int id) {
  status[id] = VISITED;
}
void mark(int id) {
  status[id] = MARKED;
}

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

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    visit(u);
    for (int i = 0; i < N; i++) { 
      if (ADJ[u][i] && status[i] == ACTIVE) {
        q.push(i);
        mark(i);
        depth[i] = depth[u] + 1;
      }
    }
  }
}

int main() {
  cin >> N;
  int id, nn, x;
  for (int i = 0; i < N; i++) {
    cin >> id >> nn;
    id--; // 0-based
    for (int j = 0; j < nn; j++) {
      cin >> x;
      x--; // 0-based
      ADJ[id][x] = 1;
    }
  }

  bfs(0);
  for (int i = 0; i < N; i++)
    cout << i << " " << depth[i] << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define vv vector< vector< int > >
#define ll long long


int main(){
    int n;
    cin >> n;
    vv tree(n);
    for(int i = 0; i < n; ++i){
        int a, k;
        cin >> a >> k;
        a--;
        for(int j = 0; j < k; ++j){
            int b;
            cin >> b;
            b--;
            tree[a].push_back(b);
        }
    }
    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 : tree[v]){
            if(dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    for(int v = 0; v < n; v++) cout << v+1 << " " << dist[v] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<int> graph[105];
bool vis[105];
int lvl[105];
int i;
void bfs(int rt)
{
    int src,adj;
    src=rt;
    deque<int>dq;
    vis[src]=1;
    lvl[src]=1;
    dq.push_back(src);
    while(!dq.empty())
    {
        src=dq.front();
        dq.pop_front();
        for(i=0; i<graph[src].size(); i++)
        {
            adj=graph[src][i];
            if(vis[adj]==0)
            {
                vis[adj]=1;
                lvl[adj]=lvl[src]+1;
                dq.push_back(adj);
            }
        }
    }
}
int main()
{
    int nodes,x,v;
    cin >> nodes;
    int u[200];
    memset(vis,0,sizeof(vis));
    memset(lvl,0,sizeof(lvl));
    for(i=1;i<=nodes;i++)
    {
        cin >> u[i];
        cin >> x;
        for(int j=0;j<x;j++)
        {
            cin >> v;
            graph[u[i]].push_back(v);
        }
    }
    bfs(1);
    for(i=1;i<=nodes;i++)
    {
        cout << u[i] << " " << lvl[u[i]]-1 << endl;
    }
}

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

using namespace std;

enum COLOR {
  WHITE,
  GRAY,
  BLACK,
};

static const int INFTY = INT_MAX;
static const int N = 100;

static int M[N][N] = {};
static vector<COLOR> color(N, WHITE);
static vector<int> d(N, INFTY);

static void bfs(int n, int s) {
  color[s] = GRAY;
  d[s] = 0;

  queue<int> Q;
  Q.push(s);

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

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

  int number, count, value;
  for (int i = 0; i < n; i++) {
    cin >> number >> count;
    number--;
    for (int j = 0; j < count; j++) {
     cin >> value;
     value--;
     M[number][value] = 1; 
    }
  }
  
  bfs(n, 0);
  
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? -1 : d[i]) << endl; 
  }

  return 0;
}