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<<21);

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 (!p.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() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

  int u, k, v;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--; // to 0 origin
    for (int j = 0; j < n; j++) {
      cin >> v;
      v--; // to 0 origin
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <stdio.h>

#define N 100     
#define SUCCESS 1 // 成功
#define FAILURE 0 // 失敗
#define INFTY 1 << 21 // 到達不可能を表す大きな値

int queue[N]; // キューの本体
int queue_head = 0; // データ先頭
int queue_num; // データ個数

int node_num, M[N][N];
int d[N]; // 距離で訪問状態(color)を管理する

int enqueue(int enq_data) // キューにデータを追加する
{
  if (queue_head + queue_num < N) {
    queue[queue_head + queue_num] = enq_data;
    queue_num++;
    return SUCCESS;
  } else {
    return FAILURE;
  }
}

int dequeue(int *deq_data) // キューからデータを取り出す
{
  if (queue_num > 0) {
    *deq_data = queue[queue_head];
    queue_num--;
    queue_head++;
    return SUCCESS;
  } else {
    return FAILURE;
  }
}

void bfs(int s) {
  enqueue(s); // 始点をキューに入れる
  for (int i = 1; i <= node_num; i++) d[i] = INFTY; // とりあえず全部INFTYにする
  d[s] = 0; // 始点から自分自身までの距離は0
  int u, p;
  while(queue_num > 0) { // キューが空でない間繰り返す
    u = queue[queue_head]; // キューの先頭からノードを取り出し，訪問
    dequeue(&p); // 同上
    for (int v = 1; v <= node_num; v++) {
        if (M[u][v] == 0) continue; // キュー先頭のノードとvが繋がってないならこのループは終了
        if (d[v] != INFTY) continue; // 既に距離を決めているならこのループは終了
        d[v] = d[u] + 1; // 次に訪問するノードまでの距離は，それまでの距離+1
        enqueue(v); // uに繋がっているノードは将来訪問しないといけない
    }
  }
  for (int i = 1; i <= node_num; i++) { // 表示部
    printf("%d %d\n", i, d[i]);
  }
}

// mainの流れはdfsと同じ(隣接行列の初期化→隣接行列にノード間の接続関係を登録→bfsを呼び出す)
int main(void)
{
  int u, v, k, i, j;
  
  scanf("%d", &node_num);
  for (i = 0; i <= node_num; i++) {
    for(j = 0; j <= node_num; j++) M[i][j] = 0;
  }

  for (i = 0; i < node_num; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d", &v);
      M[u][v] = 1;
    }
  }

  bfs(1);
  
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,lis[111][111]={},d[111];
  for(int i=0;i<111;i++)d[i]=-1;
  cin>>n;
  for(int c=0;c<n;c++){
    int u,k,v;
    cin>>u>>k;
    for(int i=0;i<k;i++){
      cin>>v;
      lis[u][v]=1;
    }
  }
  queue< pair<int,int> > q;
  q.push(make_pair(1,0));
  while(!q.empty()){
    int from=q.front().first;
    int m=q.front().second;
    q.pop();
    if(d[from]==-1){
      d[from]=m;
      for(int i=1;i<=n;i++){
	if(lis[from][i])q.push(make_pair(i,m+1));
      }
    }
  }
  for(int i=1;i<=n;i++)cout<<i<<" "<<d[i]<<endl;
  return 0;
}

C++: #include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdio.h>
#define REP(i, n) for(int i = 0;i < n;i++)

using namespace std;

static const int N = 100;
static const int INFTY = (1 << 21);

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

void bfs(int s)
{
	queue<int>	q;
	q.push(s);
	REP(i, n)
		d[i] = INFTY;
	d[s] = 0;
	int u;

	while (!q.empty)
	{
		u = q.front();
		q.pop();
		REP(v, n)
		{
			if (M[u][v] == 0)
				continue;
			if (d[v] != INFTY)
				continue;
			d[v] = d[u]++;
			q.push(v);
		}
	}
	REP(i, n)
		cout << i++ << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
}

int main()
{
	int u, k, v;

	cin >> n;

	REP(i, n)
	{
		REP(j, n)
			M[n][n] = 0;
	}

	REP(i, n)
	{
		cin >> u >> k;
		u--;
		REP(i, k)
		{
			cin >> v;
			v--;
			M[u][v] = 1;
		}
	}

	bfs(0);
	return 0;
}

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

using namespace std;

int n;
int adja[101][101];
int len[101];

void bfs() {
    queue<int> q;
    q.push(1);
    int d = 0;
    fill(len, &len[101], -1);
    while(!q.empty()) {
        for (int k = 0, size = q.size(); k < size; ++k) {
            int t = q.front();
            q.pop();
            if (len[t] > 0)
                continue;
            len[t] = d;
            for (int i = 1; i <= n; ++i) {
                if (adja[t][i] && len[i] < 0)
                    q.push(i);
            }
        }
        d++;
    }
}

int main() {
    int u, k, v;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> u >> k;
        while (k--) {
            cin >> v;
            adja[u][v] = 1;
        }
    }
    bfs();
    for (int i = 1; i <= n; ++i) {
        cout << i << " " << len[i] << endl;
    }

    return 0;
}