Go: package main

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

type AdjacentList struct {
	matrix [][]int
}

func NewEmptyAdjacentList(dim int) *AdjacentList {
	matrix := make([][]int, dim)
	for i := 0; i < dim; i++ {
		matrix[i] = make([]int, dim)
	}
	return &AdjacentList{matrix: matrix}
}

func (al *AdjacentList) set(node int, adjacent int) {
	al.matrix[node-1][adjacent-1] = 1
}

func (al *AdjacentList) findAdjacentsFor(node int) []int {
	adjacents := []int{}
	for i, v := range al.matrix[node-1] {
		if v == 1 {
			adjacents = append(adjacents, i+1)
		}
	}
	return adjacents
}

type DistanceList struct {
	list []int
}

func NewDistanceList(dim int) *DistanceList {
	list := make([]int, dim)
	for i := 0; i < dim; i++ {
		list[i] = -1
	}
	return &DistanceList{list: list}
}

func (dl *DistanceList) set(node int, distance int) {
	dl.list[node-1] = distance
}

func (dl *DistanceList) get(node int) int {
	return dl.list[node-1]
}

func (dl *DistanceList) isVisited(node int) bool {
	return dl.list[node-1] >= 0
}

type Position struct {
	node     int
	distance int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	dim, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	al := NewEmptyAdjacentList(dim)

	for i := 1; i <= dim; i++ {
		node, err := nextInt(sc)
		if err != nil {
			panic(err)
		}

		adjacentNum, err := nextInt(sc)
		if err != nil {
			panic(err)
		}
		for j := 0; j < adjacentNum; j++ {
			adjacent, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			al.set(node, adjacent)
		}
	}

	dl := NewDistanceList(dim)

	bfs(al, dl)

	for i := 1; i <= dim; i++ {
		fmt.Printf("%d %d\n", i, dl.get(i))
	}
}

func bfs(al *AdjacentList, dl *DistanceList) {
	stack := []Position{Position{node: 1, distance: 0}}
	for len(stack) > 0 {
		currentPosition := stack[0]
		stack = stack[1:]
		if !dl.isVisited(currentPosition.node) {
			dl.set(currentPosition.node, currentPosition.distance)
		}
		for _, v := range al.findAdjacentsFor(currentPosition.node) {
			if !dl.isVisited(v) {
				stack = append(stack, Position{node: v, distance: currentPosition.distance + 1})
			}
		}
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

vector< vector < int > > adjList;
vector< int > depths;

void bfs(void) {
  queue< int > currentLevel;
  queue< int > nextLevel;
  nextLevel.push(1);
  int depth = 0;
  while (nextLevel.size()) {
    while (nextLevel.size()) {
      currentLevel.push(nextLevel.front());
      nextLevel.pop();
    }
    while (currentLevel.size()) {
      if (depths[currentLevel.front()-1] == -1) {
	depths[currentLevel.front()-1] = depth;
	for (int i = 0; i < adjList[currentLevel.front()-1].size(); i++)
	  nextLevel.push(adjList[currentLevel.front()-1][i]);
      }
      currentLevel.pop();
    }
    depth++;
  }
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjList = vector< vector < int > > (n, vector< int > ());
  depths = vector< int > (n, -1);
  for (int i = 0; i < n; i++) {
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      int holder;
      cin >> holder;
      adjList[i].push_back(holder);
    }
  }
  bfs();
  for (int i = 0; i < n; i++)
    cout << i+1 << ' ' << depths[i] << endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <string.h>

/*
 * ref. http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B
 */

using namespace std;

const int MAX_N = 100;

int n;
vector<int> adjlist[MAX_N];
int d[MAX_N];
queue<int> que;

void bfs() {
    int id = 1;

    d[id - 1] = 0;
    // for (int i = 0; i < (int) adjlist[id - 1].size(); i++) {
    //     int next_id = adjlist[id - 1][i];
    //     if (d[next_id - 1] < 0 || d[next_id - 1] > d[id - 1] + 1) {
    //         d[next_id - 1] = d[id - 1] + 1;
    //         que.push(next_id);
    //     }
    // }
    que.push(id);

    while (!que.empty()) {
        id = que.front();
        que.pop();

        for (int i = 0; i < (int) adjlist[id - 1].size(); i++) {
            int next_id = adjlist[id - 1][i];
            if (d[next_id - 1] < 0 || d[next_id - 1] > d[id - 1] + 1) {
                d[next_id - 1] = d[id - 1] + 1;
                que.push(next_id);
            }
        }
    }
}

int main(void) {
    cin >> n;

    for (int i = 0; i < n; i++) {
        int id, m;
        cin >> id >> m;
        for (int j = 0; j < m; j++) {
            int x;
            cin >> x;
            adjlist[id - 1].push_back(x);
        }
    }

    memset(d, -1, sizeof(d));
    bfs();

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

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int MOD =1e9+7;
using ll=long long;
using Graph = vector<vector<int>>;

int main() {
    int n; 
    cin >> n;
    Graph G(n);
    
    rep(i,n){
        int N; cin >> N; N--;
        int k; cin >> k;
        rep(j,k){
            int v; cin >> v; v--;
            G[N].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(int nv :G[v]){
            if(dist[nv] != -1) continue;

            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i,n){
        cout << i+1 <<" "<< dist[i] << endl;
    }
}

C++: #include <cstdio>
#include <queue>

using namespace std;

#define N 100
#define INF -1

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

void bfs(int s){
	for(int i=0;i<n;i++){
		d[i] = INF;
	}
	int u,v;
	queue<int> q;
	q.push(s);
	d[s] = 0;
	while( !q.empty() ){
		u = q.front();
		q.pop();
		for(v=0;v<n;v++){
			if(d[v] != INF || m[u][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(void){
	int u,k,v;
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			m[i][j] = 0;
		}
	}
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d %d",&u,&k);
		u--;
		for(int j=0;j<k;j++){
			scanf("%d",&v);
			v--;
			m[u][v] = 1;	
		}
	}
	bfs(0);
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
bool a[100][100];
int D[100];
void  bfs ( int src) {
	queue<int> Q;
	Q.push(src);
	D[src]=0;
	while( ! Q.empty() ) {
		int cur = Q.front();
		Q.pop();
		for(int dst = 0; dst < n; dst++ ) {
			if( D[dst] == 0 && a[cur][dst] == 1 ) {
				D[dst] = D[cur] + 1;
				Q.push(dst);
			}
		}
	}
	for (int i = 0; i < n;i++) {
	cerr << i+1 <<  " " << D[i] <<  endl;
	}
}
int main() {
	cin >> n;
	for ( int i = 0;i < n;i++) {
		for(int j= 0; j < n;j++) {
			a[i][j] = 0;
		}
	}
	for (int i = 0;i < n; i++){
		cin >> m;
		cin >> l;
		for(int j = 0; j < l;j++) {
			cin >> p;
			a[m-1][p-1] = 1;
		}
	}
	bfs(0);
}

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

using namespace std;

int d[100];
vector<int> adj[100];

void bfs(int n, int start) {
	queue<int> que;

	for (int i = 0; i < n; i++)
		d[i] = i == start ? 0 : -1;

	que.push(start);
	while(!que.empty()) {
		int from = que.front();
		que.pop();

		for (int& to : adj[from])
			if (d[to] == -1) {
				d[to] = d[from] + 1;
				que.push(to);
			}
	}
}

int main() {
	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		while (k--) {
			int v;
			cin >> v;
			adj[u - 1].push_back(v - 1);
		}
	}

	bfs(n, 0);

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

	return 0;
}