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 <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == u) return 0;
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		if (d_min == -1) d_min = next[i]->breadthFirstSearch(id) + 1;
		else d_min = min(next[i]->breadthFirstSearch(id) + 1, d_min);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);

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

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

void bfs(int s){
  queue<int> q;
  q.push(s);

  REP(i,n) d[i] = INF;

  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] != INF) continue;

      d[v] = d[u] + 1;
      q.push(v);
    }
  }

  REP(i,n){
    cout << i + 1 << " " << ((d[i] == INF) ? (-1) : d[i] ) << endl;
  }




}
int main(){
  int u, k, v;
  cin >> n;
  REP(i,n){
    REP(j,n) M[i][j] = 0;
  }

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

  bfs(0);
  return 0;
}

C++: 
//push(),pop():C++ではstackと統一されいる
//front():先頭の要素が何かを返す
//back():末尾の要素が何であるかを返す
//empty():キューが空であるかどうかを調べる
//size():要素数を調べる
#include<stdio.h>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 101
#define WHITE 0
#define GRAY 1
#define BLACK 2
static const int INFTY = (1<<21);

int G[MAX][MAX],color[MAX],d[MAX],num;

void bfs(int s){
  int u;
  queue<int> Q; //Qを使うことでエンキュー、デキューの操作が容易になる
  for(int i=1; i<=num; i++) {
    d[i] = INFTY;
  }
  Q.push(s);
  d[s] = 0;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();
    for(int v=1; v<=num; v++){
      if(G[u][v] == 0) continue;
      if(d[v] != INFTY) continue;
      d[v] = d[u] + 1;
      Q.push(v);
    }
  }
}


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

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

  bfs(1);

  for(i=1; i<=num; i++) {
    if(d[i] == INFTY) printf("%d -1\n",i);
    else printf("%d %d\n",i,d[i]);
  }

  return 0;
}

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

#define MAX 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n;
bool G[MAX + 1][MAX + 1];
int col[MAX + 1];
int d[MAX + 1];

void init(int n) {
	for (int i = 1; i <= n; i++) {
		col[i] = WHITE;
		d[i] = -1;
		for (int j = 1; j <= n; j++)
			G[i][j] = 0;
	}
}

void bfs(int u) {
	queue<int> Q;
	Q.push(u);
	col[u] = GRAY;
	d[u] = 0;
	while (!Q.empty()) {
		int v = Q.front();
		Q.pop();
		for (int i = 1; i <= n; i++) {
			if (G[v][i] && col[i] == WHITE) {
				Q.push(i);
				col[i] = GRAY;
				d[i] = d[v] + 1;
			}
		}
		col[v] = BLACK;
	}
}

int main() {
	cin >> n;
	init(n);
	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			int v;
			cin >> v;
			G[u][v] = 1;
		}
	}
	bfs(1);
	for (int u = 1; u <= n; u++) {
		cout << u << " " << d[u] << endl;
	}
	return 0;
}

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


int main(){
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  for(int i=0;i<n;++i){
    int id,k;
    cin >> id >> k;
    id--;
    for(int j=0;j<k;++j){
      int v;
      cin >> v;
      v--;
      g[id].push_back(v);
    }
  }
  vector<int> dist(n,-1);
  queue<int> que;
  dist[0]=0;
  que.push(0);
  while(!que.empty()){
    int v=que.front();
    que.pop();
    for(auto nv : g[v]){
      if(dist[nv]!=-1)continue;
      dist[nv]=dist[v]+1;
      que.push(nv);
    }
  }
  for(int i=0;i<n;++i){
    cout << i+1 << " " << dist[i] << endl;
  }
  return 0;
}