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<cstdio>
#define INF 1<<30
#define MAX 100000
using namespace std;
  
class Queue{
private:
  int inf[MAX];
  int head,tail;
   
public:
   
  Queue(){
    syokika();
  }
   
  void syokika(){
    head = 0;
    tail = 0;
  }
   
  void push(int x){
    inf[tail]=x;
    if(MAX == tail+1)tail = 0;
    else tail++;
  }
   
  void pop(){
    if(MAX == head+1)head=0;
    else head++;
  }
   
  int front(){
    return inf[head];
  }
   
  bool empty(){
    if(tail - head == 0)return true;
    else return false;
  }
   
  int size(){
    return tail - head;
  }
   
};
  
int imap[200][200];
int k[200] = {0};
int ans[200] = {0};
bool used[200];
  
int main(){
  int n,u[1000];
  cin >> n;
  for(int i=0;i<200;i++)for(int j=0;j<200;j++)imap[j][i] = INF;
  
  for(int i=0;i<200;i++){
    k[i]=ans[i]=u[i]=-1;
  }
  
  for(int i=0;i<n;i++){
    cin >> u[i];
    cin >> k[u[i]];
    for(int j=0;j<k[u[i]];j++){
      cin >> imap[u[i]][j];
    }
  }
  
   int h;
  for(int i=0;i<n;i++){
    int Min = i;
    for(int j=i;j<n;j++){
      if(u[Min] > u[j])Min = j;
    }
    h = u[Min];
    u[Min] = u[i];
    u[i] = h;
  }
  
  Queue que = Queue();
  
  que.push(u[0]);
  ans[u[0]]=0;
  
  while(!que.empty()){
    int p = que.front(); que.pop();
    for(int i=0;i<k[p];i++){
      if(!used[imap[p][i]]){
    que.push(imap[p][i]);//cout << "push!= " << imap[p][i] << endl;
    used[imap[p][i]] = true;
    ans[imap[p][i]] = ans[p] + 1;
      }
    }
  }
  //for(int i=0;i<n;i++)cout << u[i] << endl;
   
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ans[u[i]] << endl;
  }
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int INF=(1<<21);
int m[101][101],n;
int d[101]; //最短距離を記録
void bfs(int s){
  queue<int>q;
  q.push(s); //始点sを入れる（訪問）
  for(int i=0;i<n;i++) d[i]=INF;//予めINFを入れておく
  d[s]=0;
  while(!q.empty()){
    int u=q.front();
    q.pop(); //キューから頂点uを取り出し、訪問
    for(int v=0;v<n;v++){
      if(m[u][v]==0) continue;
      //辺がない
      if(d[v]!=INF) continue;
      //訪問したことがある
      d[v]=d[u]+1; //vは未訪問の頂点であり、隣接しているため、d[u]に1を足す
      q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout<<i+1<<" "<<( (d[i]==INF)?(-1):d[i] )<<endl;
  }
}
int main(){
  int u,k,v;
  scanf("%d",&n);
   for(int i=0;i<n;i++){
    scanf("%d %d",&u,&k);
    u--;
    for(int j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      m[u][v]=1;
    }
  }
   bfs(0);
   return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;


bool is_connected[150][150];
int min_distance[150];

void init() {
    int i, j;

    rep(i, 150) {
        min_distance[i] = 150;
    }
    min_distance[0] = 0;
    min_distance[1] = 0;
    rep(i, 150) rep(j, 150) is_connected[i][j] = false;
}

void solveDepth(int src_id, int cur_depth) {
    int dst_id;
    rep(dst_id, 150) {
        if (is_connected[src_id][dst_id]) {
            if (cur_depth + 1 < min_distance[dst_id]) {
                min_distance[dst_id] = cur_depth + 1;
                solveDepth(dst_id, cur_depth + 1);
            }
        }
    }
}

int main() {
    int n, i, j, u, v, k;
    cin >> n;

    init();

    rep(i, n) {
        cin >> u >> k;
        rep(j, k) {
            cin >> v;
            is_connected[u][v] = true;
        }
    }

    solveDepth(1, 0);

    rep(i, n) {
        cout << i + 1 << " ";
        if (min_distance[i + 1] == 150) {
            cout << -1 << endl;
        } else {
            cout << min_distance[i + 1] << endl;
        }
        
    }

    return 0;
}