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) {
			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++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_B

#include <iostream>
#include <iomanip>

#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <queue>

typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)

using namespace std;

// change min/max
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

template<class T> void output(std::vector<T> & A) {
    const int n = A.size();
    for (int i = 0; i < n - 1; ++i) {
        std::cout << A[i] << " ";
    }
    std::cout << A[n - 1] << std::endl;
}

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
vector<vector<int>> M;
vector<int> colour, d;
int tt = 0;
queue<int> que;

void bfs(int u) {
    que.push(u);
    colour[u] = GRAY;

    while (!que.empty()) {
        const int v = que.front(); que.pop();
        for (int s : M[v]) {
            if (colour[s] == WHITE) {
                que.push(s);
                d[s] = d[v] + 1;
                colour[s] = GRAY;
            }
        }
        colour[v] = BLACK;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout << std::fixed << std::setprecision(15);

    // populate M
    int n; cin >> n;
    M = vector<vector<int>>(n);
    colour = vector<int>(n, 0);
    d = vector<int>(n, -1);

    rep(i, n) {
        int from; cin >> from; --from;
        int m; cin >> m;
        rep(j, m) {
            int to; cin >> to; --to;
            M[from].push_back(to);
            // M[to].push_back(from);
        }
    }

    // bfs
    d[0] = 0;
    bfs(0);

    rep(i, n) {
        cout << (i + 1) << " " << d[i] << endl;
    }

    return 0;
}

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

using namespace std;

int n;
int u[10010], k[10010],  v[10010][10010];
int answer[10010][10010];

int D[10010];
void bfs(int src){
	//cerr << "bfs root = " << src << endl;
	queue<int> Q;
	Q.push(src);
	D[src] = 0;
	//int cnt = 0;
	while(! Q.empty()){
		int cur = Q.front();
		Q.pop();
		//cerr << "visiting " << cur << " " << D[cur] << endl;
		
		for(int dst = 0; dst < 1001; ++dst){
			if(answer[cur - 1][dst - 1] == 1 && D[dst] > 10000){
				D[dst] = D[cur] + 1; 
				Q.push(dst);
			}
		}

	}
	for(int i = 1; i  < n + 1; ++i ){
		if(D[i] < 10000){
			cout << i << " " <<  D[i] << endl;
		}else{
			cout << i << " " << -1 << endl;
		}
	}

	
}

int main(){
	cin >> n;
	for(int i = 0; i < 1001; ++i){
		for(int j = 0; j < 1001; ++j)
			answer[i][j] = 0;
	}
	for(int i = 0; i < 10010; ++i){
		D[i] = 10010;
	}

	
	
	for(int i = 0; i < n; ++i){
		cin >> u[i] >> k[i];
		for(int j = 0; j < k[i]; ++j){
			cin >> v[i][j];
			answer[u[i] - 1][v[i][j] - 1] = 1;
		} 
	}
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j){
			//cout << answer[i][j] << " ";
		} 
		//cout << endl;
	}
	bfs(1);
}

C++: #include <iostream>
using namespace	std;
int n,g[100][100],f[100],c[100];
void bfs(int now,int far){
  f[now]=far;
  c[now]=1;
  far++;
  for(int i=0;i<n;i++) if(g[now][i]&&f[i]!=1) bfs(i,far);

}
int main(){

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    for(int j=0;j<b;j++){
      cin>>tmp;
      g[a-1][tmp-1]=1;
    }
  }

  bfs(0,0);

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

  return 0;

}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
  
  int n,a,b,c,ans[101];
  vector<int> vec[101];
  queue<int> que;
  
  fill(ans,ans+101,105);
  ans[1] = 0;
  
  cin >> n;
  for(int i=0;i<n;i++){ 
    cin >> a >> b;
    for(int j=0;j<b;j++){
      cin >> c;
      vec[a].push_back(c);
    }
  }

  que.push(1);
  
  while(!que.empty()){
    
    int d = que.front();
    que.pop();
    
    for(int i=0;i<vec[d].size();i++){
      int e = vec[d][i];
      if(ans[e] > ans[d]+1){
	ans[e] = ans[d]+1;
	que.push(e);
      }
    }
    
  }
  for(int i=1;i<=n;i++) cout << i << ' ' << ans[i] << endl;
  return (0);
}

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

using namespace std;

int p[100+10][100+10];
int D[100+10],e[100+10];
int n,a,b,c;

int main(){
	cin >> n;
	for (int i=1; i<=n; i++){
		cin >> a >> b;
		for (int j=1; j<=b; j++){
			cin >> c;
			p[a][c] = 1;
		}
	}
	
	queue<int> Q;
	Q.push(1);
	D[1] = 0;
	e[1] = 1;
	while(!Q.empty()){
		int m = Q.front();
		Q.pop();
		for (int i=1; i<=n; i++){
			if (p[m][i]==1 && e[i]==0){
				D[i] = D[m]+1;
				Q.push(i);
				e[i] = 1;
			}
		}
	}
	
	for (int i=1; i<=n; i++){
		cout << i << ' ' << D[i] << endl;
	}
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n,0));
    rep(i,n){
        int u,k;
        cin >> u >> k;
        u--;
        rep(i,k){
            int v;
            cin >> v;
            v--;
            G[u][v] = 1;
        }
    }
    queue<int> q;
    vector<int> d(n,-1);
    d[0] = 0;
    q.push(0);
    while(!q.empty()){
        int p = q.front();
        q.pop();
        rep(i,n){
            if(G[p][i] == 1 && d[i] == -1){
                //cout << G[p][i] << " " << d[i] << endl;
                q.push(i);
                d[i] = d[p]+1;
                //cout << d[i] << endl;
            }
        }
    }
    rep(i,n){
        cout << i+1 << " " << d[i] << endl;
    }
    return 0;
}