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>
#include<string>
#include<cctype>
#include<cmath>
#include<queue> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
  queue <int> q1;
  int n,i,j;
  cin >> n;
  int d[n][n];
  int m[n];
  rep(i,n){
    m[i]=10000;
    rep(j,n){
      if(i==j){
        d[i][j]=0;
      }else{
        d[i][j]=10000;
      }
    }
  }
  m[0]=0;

  rep(i,n){
    int u,k;
    cin >> u >> k;
    if(k>0){
      rep(j,k){
        int v;
        cin >> v;
        if(d[v-1][u-1]!=1){
          d[u-1][v-1]=1;
        }
      }
    }
  }   
  q1.push(0);
  rep(i,n){
    int u = q1.front();
    rep(j,n){
      if(d[u][j]==1){
        q1.push(j);
        m[j] = min(m[j],m[u]+1);
      }
    }
    q1.pop();
  }

  rep(i,n){
    cout << i+1 << ' ';
    if(m[i]==10000){
      cout << -1 << endl;
    }else{
      cout << m[i] << endl;
    }
  }

  return 0;
}

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

struct Node {
	int ID;
	int d;
	bool visited;
	bool operator<(const Node& rhs) {return this->ID < rhs.ID;}
	bool operator>(const Node& rhs) {return this->ID > rhs.ID;}
	bool operator<=(const Node& rhs) {return this->ID <= rhs.ID;}
	bool operator>=(const Node& rhs) {return this->ID >= rhs.ID;}
	bool operator==(const Node& rhs) {return this->ID == rhs.ID;}
	bool operator!=(const Node& rhs) {return this->ID != rhs.ID;}
};

void BFS(std::vector<int>* adjlist, Node* nodes, std::queue<int> que)
{	
	nodes[que.front()].d = 0;
	
	while (!que.empty()) {
		int id = que.front(); que.pop();
		for (std::vector<int>::iterator it = adjlist[id].begin(); it != adjlist[id].end(); ++it) {
			if (!nodes[*it].visited) { 
				que.push(*it);
				nodes[*it].d = nodes[id].d + 1;
				nodes[*it].visited = true;
			}
		}
	}
}

int main()
{
	int n; std::cin >> n;
	
	std::vector<int>* adjlist = new std::vector<int>[n];
	Node* nodes = new Node[n];
	
	std::queue<int> que;
	
	for (int i=0; i<n; i++) {
		nodes[i].visited = false; nodes[i].d = -1;
	}
	
	for (int i=0; i<n; i++) {
		int x;
		int deg;
		std::cin >> x >> deg;
		nodes[i].ID = x-1;
		for (int j=0; j<deg; j++) {
			int y;
			std::cin >> y;
			adjlist[x-1].push_back(y-1);
		}
	}
	
	std::sort(nodes, nodes + n);
	

	que.push(0); nodes[0].visited = true;
	BFS(adjlist, nodes, que);
	
	
	
	for (int i=0; i<n; i++) {
		std::cout << nodes[i].ID + 1 << " " << nodes[i].d << std::endl;
	}
	
	delete[] adjlist;
	delete[] nodes;
	
	return 0;
}

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

int graph[101][101];
int dis[101]; //距離を格納
bool check[101];//訪問済みチェック
int main(){
  int n;
  cin>>n;
  fill(dis,dis+101,INF);
  dis[1]=0;
  check[1]=true;
  for(int i=0;i<n;i++){
    int u,k,v;
    cin>>u>>k;
    for(int j=1;j<=k;j++){
      graph[u][0]=k;
      cin>>graph[u][j];
    }
  }
  queue<int> qu;
  qu.push(1);
  while(!qu.empty()){
    int num=qu.front(); qu.pop();
    for(int j=1;j<=graph[num][0];j++){
      int temp=graph[num][j];
      if(check[temp]){//訪問済みならば
        continue;
      }else{
        check[temp]=true;
      }
      dis[temp]=dis[num]+1;
      qu.push(temp);
    }
  }
  //表示部
  for(int i=1;i<=n;i++){
    cout<<i<<" ";
    if(dis[i]>=INF)cout<<-1<<"\n";
    else  cout<<dis[i]<<"\n";
  }
}

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

int D[101]; 
int edges[101][101];

void bfs(int src,int n)
{
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while(!Q.empty())
	{
		int cur=Q.front();
		Q.pop();
		for(int dst=1;dst<=n;dst++)
		{
			int flag=0;
			for(int i=0;i<n;i++)
			{
				if(edges[cur][i]==dst)
				{
					flag=1;
					break;
				}
			}
			if(D[dst]==-1&&flag)
			{
				D[dst]=D[cur]+1;
				Q.push(dst);
			}
		}
	}
}

int main()
{
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
	{
		int u,k;
		cin >> u >> k;
		for(int j=0;j<k;j++)
		   cin >> edges[u][j];
	}
	for(int i=1;i<=n;i++)
	   D[i]=-1;
	bfs(1,n);
	for(int i=1;i<=n;i++)
	   cout << i << ' ' << D[i] << 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;
}