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++: #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>
using namespace std;

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

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for (int i = 0; i < n; i++) d[i] = (1 << 21);
    d[s] = 0;
    int u;
    while(!q.empty()) {
        u = q.front();
        q.pop();
        for (int v = 0; v < n; v++) {
            if (N[u][v] == 0) continue;
            if (d[v] != (1 << 21)) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i + 1 << " " << ( (d[i] == (1 << 21)) ? (-1) : d[i]) << endl;
    }
}

int main() {
    int u, k, v;
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0 ; j < n; j++) {
            N[i][j] = 0;
        }
    }
    
    for (int i = 0; i < n; i++) {
        cin >> u >> k;
        if (k != 0) {
            for (int j = 0; j < k; j++) {
                cin >> v;
                N[u-1][v-1] = 1;
            }
        }
    }
    bfs(0);
    
    return 0;
}

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

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

int main() {
	int n;
	cin >> n;
	int dist[n];
	vector<int> map[n];
	for (int i = 0; i < n; i++) {
		dist[i] = -1;
		int a, b, c;
		cin >> a >> c;
		for (int j = 0; j < c; j++) {
			cin >> b;
			map[a - 1].push_back(b - 1);
		}
	}
	
	queue<int> q;
	q.push(0);
	dist[0] = 0;
	while (q.size() > 0) {
		int c = q.front(); q.pop();
		for (int i = 0; i < map[c].size(); i++) {
			if (dist[map[c][i]] != -1) continue;
			dist[map[c][i]] = dist[c] + 1;
			q.push(map[c][i]);
		}
	}
	
	for (int i = 0; i < n; i++)
		cout << (i + 1) << " " << dist[i] << endl;
	return 0;
}

C++:  #include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
#define _GLIBCXX_DEBUG
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,u,v,k;
    cin>>n;
    vector<vector<int> > lis(n+5);
    vector<bool> flag(n+5,false);
    rep(i,n){
        cin>>u>>k;
        rep(j,k){
            cin>>v;
            lis[u].push_back(v);
        }
    }
    queue<int> que;
    vector<int> time(n+5,-1);
    int now=0;
    que.push(1);
    time[1]=0;
    while(!que.empty()){
        int nowp=que.front();
        que.pop();
        if(flag[nowp]){
        }
        else{
            flag[nowp]=true;
            rep(i,lis[nowp].size()){
                que.push(lis[nowp][i]);
                if(time[lis[nowp][i]]==-1){
                    time[lis[nowp][i]]=time[nowp]+1;      
                }
            }
        }
    }
    REP(i,1,n+1){
        cout<<i<<' '<<time[i]<<'\n';
    }
    return(0);
}

C++: #include <bits/stdc++.h>
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define FORq(i, m, n) for(int i = m;i <= n;i++)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define PRINTD(n) printf("%d\n",n)
#define DEBUG printf("%s\n","debug")
using namespace std;
 /////////////////////////////////////////////////////
const int V_MAX = 102;
bool adj[V_MAX][V_MAX];
int d[V_MAX] = {};

int main(){
	int n;SCD(n);
	FORq(i,1,n){
		int u,k; SCD2(u,k);
			FORq(j,1,k){
				int v; SCD(v);
				adj[u][v] = true;
			}
		
	}

	
	REP(i,V_MAX) {d[i] = -1;}
	
	queue<int> Q;
	Q.push(1);
	d[1] = 0;
	
	while(!Q.empty()){
		int now = Q.front(); 
		Q.pop();
		int v = d[now];
		FORq(i,1,n){
			if ((adj[now][i] == true) and (d[i] == -1)){
				d[i] = v+1;
				Q.push(i);
			}
		}
	}
	
	FORq(i,1,n){
		printf("%d %d\n",i,d[i]);
	}
	
	
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int adj[105][105] = {0};
int res[105] = {0};
int n;

int main() {
    cin >> n;
    REP(i, n){
        int u, k;
        cin >> u >> k;
        REP(j, k) {
            int tmp;
            cin >> tmp;
            adj[u-1][tmp-1] = 1;
        }
        res[i] = INF;
    }
    res[0] = 0;
    REP(k, 3) REP(i, n) REP(j, n)
        if(adj[i][j] != 0)
            res[j] = min(res[j], res[i]+1);

    REP(i, n){
        if(res[i] != INF)
            cout << i+1 << " " << res[i] << endl;
        else
            cout << i+1 << " -1" << endl;
    }
    return 0;
}