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 <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_map>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

#define INF -1

struct Node {
    int d;
    int n;
    Node(){}
    Node(int n, int d){
        this->d = d;
        this->n = n;
    }
};

int main(){
    int n; cin >>n;
    vector<int> v;
    vector<vector<int>> edges(n,v);
    rep(i, n) {
        int id; cin >> id;
        int num; cin >> num;
        rep(j, num) {
            int dst;
            cin >> dst;
            dst--;
            edges[i].push_back(dst);
         }
    }
    queue<Node> q;
    vector<int> dst(n,INF);

    q.push(Node(0,0));
    while(!q.empty()) {
        Node node = q.front();
        q.pop();
        if(dst[node.n] == INF) {
            dst[node.n] = node.d;
            // cout << "popped " << node.n << endl;
        } else {
            continue;
        }
        for(auto it = edges[node.n].begin(); it != edges[node.n].end(); it++) {
            // cout << "pushed " << *it << endl;
            q.push(Node(*it, node.d+1));
        }
    }
    rep(i, n) {
        cout << i+1 << " " << dst[i] << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<algorithm>
#include<utility>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int INF = pow(10, 8);
int main(){
  int n;cin>>n;
  vector<vector<int> > edge(n, vector<int>());
  for(int i=0;i<n;i++){
    int u, k;cin>>u>>k;u--;
    for(int j=0;j<k;j++){
      int v;cin>>v;edge[i].push_back(v-1);
    }
  }
  vector<int> dis(n, INF);
  dis[0]=0;
  queue<int> que;
  que.push(0);
  while(que.size()){
    int v = que.front();que.pop();
    for(int &u : edge[v]){
      if(dis[u]>dis[v]+1){
        dis[u]=dis[v]+1;
        que.push(u);
      }
    }
  }
  for(int i=0;i<n;i++){
    if(dis[i]==INF)cout<<i+1<<" "<<-1<<endl;
    else cout<<i+1<<" "<<dis[i]<<endl;
  }
  return 0;
}

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

int n,v,u,k,d=0;
int l[110],f[110];
int b[110][110];
queue<int> q;
void bfs(int a)
{
	while(!q.empty()){
		a=q.front();q.pop();
		for(int i=1;i<=n;i++){
			if(b[a][i]==1&&f[i]==0){
				f[i]=1;
				q.push(i);
				l[i]=l[a]+1;
			}
		}
	}
	return ;
}
int main()
{	
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>u;
		cin>>k;
		for(int j=1;j<=k;j++){
			cin>>v;
			//cout<<v<<endl;
			b[u][v]=1;
		} 
	}
	
	
	//cout<<"@"<<b[1][3]<<"@"<<endl;
	l[1]=0;
	f[1]=1;
	q.push(1);
	bfs(1);
	for(int i=1;i<=n;i++){
		if(l[i]==0&&i!=1){
			cout<<i<<" "<<-1<<endl;
			continue;
		}
		cout<<i<<" "<<l[i]<<endl;
	}
	return 0;
 }

C++: //
//  main.cpp
//  breadth_first_search
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <queue>

#define n_max 100
#define INFTY 1000
#define WHITE 0
#define GRAY 1
#define BLACK 2

using namespace std;

int M[n_max][n_max], color[n_max], d[n_max], n;

queue<int> Q;

void encode_graph(){
    int i, j, id, num, node;
    
    //initialize with 0
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){
            M[i][j] = 0;
        }
    }
    
    for (i = 0; i < n; i++){
        scanf("%d", &id);
        scanf("%d", &num);
        for(j = 0; j < num; j++){
            scanf("%d", &node);
            M[id-1][node-1] = 1;
        }
    }
}

void bfs(){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
        d[i] = INFTY;
    }
    //starting point
    int s = 0;
    color[s] = GRAY;
    d[s] = 0;
    Q.push(s);
    
    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int v = 0; v < n; v++){
            if((M[u][v]) && (color[v] == WHITE)){
                color[v] = GRAY;
                d[v] = d[u] + 1;
                Q.push(v);
            }
        }
        color[u] = BLACK;
    }
    
    
    
}

int main(int argc, const char * argv[]) {
    scanf("%d", &n);
    
    encode_graph();
    bfs();
    
    for(int i = 0; i < n; i++){
        cout<<i+1<<" "<<((d[i] == INFTY) ? (-1): (d[i]))<<endl;
    }
    
//    std::cout << "Hello, World!\n";
    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);
}