Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	nodes := make(map[int]*node)

	for i := 0; i < num; i++ {
		sc.Scan()
		id, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		nodeNum, _ := strconv.Atoi(sc.Text())
		n := node{id, make([]int, 0), -1}
		for j := 0; j < nodeNum; j++ {
			sc.Scan()
			nid, _ := strconv.Atoi(sc.Text())
			n.nexts = append(n.nexts, nid)
		}
		nodes[id] = &n
	}
	nodes[1].depth = 0

	walk(1, 0, &nodes)
	for i := 1; i < len(nodes)+1; i++ {
		fmt.Printf("%v %v\n", (*nodes[i]).id, (*nodes[i]).depth)
	}
}

var time int
var record [100][100]bool
var q Queue

func walk(nid int, from int, nodes *map[int]*node) {
	q.Enqueue((*nodes)[nid])
	for !q.IsEmpty() {
		n := q.Dequeue().(*node)
		for _, v := range n.nexts {
			n2 := (*nodes)[v]
			if n2.depth != -1 {
				continue
			}
			n2.depth = n.depth + 1
			q.Enqueue((*nodes)[v])
		}
	}
}

type node struct {
	id    int
	nexts []int
	depth int
}

// Queue is Queue
type Queue struct {
	val []interface{}
}

// Enqueue value
func (queue *Queue) Enqueue(val interface{}) {
	queue.val = append(queue.val, val)
}

// Dequeue value
func (queue *Queue) Dequeue() interface{} {
	res := queue.val[0]
	queue.val = queue.val[1:len(queue.val)]
	return res
}

// IsEmpty return true when queue is empty
func (queue *Queue) IsEmpty() bool {
	return len(queue.val) == 0
}

C++: #include <stdio.h>
#include <vector>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;
 
int main(void) {
  int n, ans = 0, inf = 10000;
  scanf("%d", &n);
  int dis[n];
  rep(i, n) dis[i] = inf;
  dis[0] = 0;
  vector< int > vec[n];
  rep(i, n) {
    int u, k;
    scanf("%d%d", &u, &k);
    u--;
    rep(j, k) {
      int v;
      scanf("%d", &v);
      v--;
      vec[u].push_back(v);
    }
  }
  rep(i, n - 1) {
    rep(j, n) {
      vector< int >::iterator it;
      for(it = vec[j].begin(); it != vec[j].end(); ++it) {
        if(dis[*it] > dis[j] + 1) dis[*it] = dis[j] + 1;
      }
    }
    /*rep(j, n) printf("%d ", dis[j]);
    printf("\n");*/
  }
  rep(i, n) {
    if(dis[i] != inf) printf("%d %d\n", i + 1, dis[i]);
    else printf("%d -1\n", i + 1);
  }
  return 0;
}

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

using namespace std;

int N, G[100][100], V[100], D[100];//G:２頂点,V:次数,D:距離

void bfs(int src){
	queue<int> Q;
	Q.push(src);
	fill(D, D + N, -1);
	D[src] = 0;
	while (!Q.empty()){
		int cur = Q.front();
		Q.pop();
		for (int k = 0; k < N; k++){
			if (G[cur][k] == 1 && D[k] == -1){
				D[k] = D[cur] + 1;
				Q.push(k);
			}
		}
	}
	for (int l = 0; l < N; l++){
		cerr << l + 1 << " " << D[l] << endl;
	}
}

int main(){
	scanf_s("%d", &N);
	int id, c;
	for (int i = 0; i < N; i++){
		scanf_s("%d", &id); id--;//配列は0から始まるので、頂点番号を-1して考える。
		scanf_s("%d", V + id);
		for (int j = 0; j < V[id]; j++){
			scanf_s("%d", &c); c--;//配列は0から始まるので、頂点番号を-1して考える。
			G[id][c] = 1;
		}
	}//データの読み込み完了。
	bfs(0);
}

C++: 
#include <iostream>
#include <stdio.h>
#include <queue>

using namespace std;

int** gmtrx;
int* v;
int total, n;

std::queue<int> que;

void operation(){
	int tmp = que.front();
	for(int i=0; i<n; ++i){
		if(gmtrx[tmp][i]){
			if(v[i]==-1){
				v[i]=v[tmp]+1;
				++total;
				que.push(i);
			}else{
				continue;
			}
		}
	}
	que.pop();
}

int main()
{
	scanf("%d",&n);

	gmtrx = new int*[n];
	v = new int [n]; //discovery

	for(int i=0; i<n; ++i){
		gmtrx[i] = new int[n];
		for(int j=0; j<n; ++j){
			gmtrx[i][j] = 0;
		}
		v[i] = -1;
	}

	for(int i=0; i<n; ++i){
		int v, num, a;
		scanf("%d %d",&v,&num);
		--v;
		for(int j=0; j<num; ++j){
			scanf("%d",&a);
			--a;
			gmtrx[v][a] = 1;
		}
	}

	que.push(0);
	v[0]=0;
	++total;
	while(que.size() && total!=n){
		operation();
	}

	for(int i=0; i<n; ++i){
		cout << i+1 << " " << v[i] << endl;
	}

	for(int i=0; i<n; ++i){
		delete[] gmtrx[i];
	}
	delete[] gmtrx;
	delete[] v;

	return 0;
}

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

int n;
int graph[100][100];
int INF = -100000007;
// 未探索はINF
int d[100];

int main() {

  cin >> n;

  // 配列を初期化
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      graph[i][j] = 0;
    }
    d[i] = INF;
  }

  // 入力を受け取る
  for(int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int v;
      cin >> v;
      graph[u - 1][v - 1] = 1;
    }
  }

  // 頂点1を設定
  queue<int> q;
  q.push(0);
  d[0] = 0;

  // 探索
  while (!q.empty()) {
    int u = q.front(); q.pop();
    for (int i = 0; i < n; i++) {
      if (graph[u][i] == 0) {
        continue;
      }
      if (d[i] != INF) {
        continue;
      }
      d[i] = d[u] + 1;
      q.push(i);
    }
  }

  // 探索結果を出力
  for (int i = 0; i < n; i++) {
    if (d[i] == INF) {
      cout << i + 1 << " -1" << endl;
    } else{
      cout << i + 1 << " " << d[i] << endl;
    }
  }

  return 0;

}