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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    //if (V[u].size() == 0) return false;
    
    // for (int i = 0; i < V[u].size(); ++i)
    //     cout << V[u][i] << ' ';
    // cout << endl;
    
    for (int i = 0; i < V[u].size(); ++i)
        bfs(V[u][i] - 1, depth + 1);
    
    d[u] = min(d[u], depth);
    
    if (V[u].size() == 0 || u == 0)
        return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <queue>
using namespace std;
static const int N = 100, WHITE = 0, GREY = 1, BLACK = 2;

int M[N][N], color[N], d[N], n, dis;
queue<int> q;


void label_not_reach()
{
	for(int u = 0; u < n; u++)
	{
		if(color[u] == WHITE) d[u] = -1;
	}
}


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




int main() 
{
	dis = 0;
	scanf("%d", &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++)
	{
		color[i] = WHITE;
		d[i] = 0;
	}
	
	for(int i = 1; i <= n; i++)
	{
		int u,k,v;
		scanf("%d %d", &u, &k);
		for(int j = 0; j < k; j++)
		{
			scanf("%d", &v);
			M[u-1][v-1] = 1;
		}
	}
	
	bfs();
	
	for(int i = 0; i < n; i++)
	{
		printf("%d %d\n", i+1, d[i]);
	}
	
//	system("pause");
	return 0;
}

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

int G[223][223];
int dis[223];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  cin >> n;
  for(int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for(int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st) {
    1, 0
  });

  while(!q.empty()) {

    st u = q.front();
    q.pop();
    if(dis[u.pos] < 1000000) continue;

    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[u.pos][i] == 0) continue;
      q.push((st) {
        i, u.v + 1
      });
    }
  }

  for(int i = 1; i <= n; i++) {
    printf("%d %d\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;
static const int N=100;

int n,M[N][N];
int d[N];


void bfs(int s){
    queue<int> Q;
    Q.push(s);
    d[s]=0;
    int u,v;
    while(!Q.empty()){
        u=Q.front();Q.pop();
        for(v=0;v<n;v++){
            if(M[u][v]==0) continue;
            if(d[v]!=-1) continue;
            d[v]=d[u]+1;
            Q.push(v);
        }
    }
    for(int i=0;i<n;i++) printf("%d %d\n",i+1,d[i]);
}

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;
        }
    }
    for(int i=0;i<n;i++) d[i]=-1;
    bfs(0);
    return 0;
}

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

int main() {
    int N; cin >> N;
    vector<vector<int>> G(N);
    rep(i, 0, N) {
        int u, k;
        cin >> u >> k;
        u--;
        rep(j, 0, k) {
            int v; cin >> v;
            v--;
            G[u].push_back(v);
        }
    }
    vector<int> dist(N, -1);
    queue<int> Q;
    Q.push(0);
    dist[0] = 0;
    while(!Q.empty()) {
        int cur = Q.front(); Q.pop();
        for(auto &to : G[cur]) {
            if(dist[to] != -1) continue;
            Q.push(to);
            dist[to] = dist[cur] + 1;
        }
    }
    rep(i, 0, N) cout << i + 1 << " " << dist[i] << '\n';
    return 0;
}