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<stdio.h>
#include<queue>
using namespace std;
int tu[100][100];
int a[100];
int d[100];
int n;
void bfs(int x){
    queue<int> q;
    q.push(x);
    for(int i=0;i<n;i++)d[i]=10000000;
    d[x]=0;
    int u;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int i=0;i<n;i++){
            if(tu[u][i]==0)continue;
            if(d[i]!=10000000)continue;
            d[i]=d[u]+1;
            q.push(i);
        }
        }
        for(int i=0;i<n;i++){
            printf("%d ",i+1);
            if(d[i]==10000000)printf("-1");
            else printf("%d",d[i]);
            printf("\n");
        }
}
int main(){
    scanf("%d",&n);
    int v,m,k;
    for(int i=0;i<n;i++){
        scanf("%d%d",&v,&m);
        v--;
        for(int j=0;j<m;j++){
            scanf("%d",&k);
            k--;
            tu[v][k]=1;
        }
    }
    bfs(0);
    return 0;
}

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

using namespace std;

typedef pair<int, int> E;

int INF = 100000000;

int main() {
  int n, u, k;
  cin >> n;
  int** G = new int*[n+1];
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    G[u] = new int[k+1];
    G[u][0] = k;
    for (int j = 1; j <= k; j++) {
      cin >> G[u][j];
    }
  }

  int cnt = 0;
  int* visit = new int[n+1];
  for (int i = 1; i <= n; i++) {
    visit[i] = INF;
  }

  queue<E> q;
  q.push(E(1, cnt));
  while (q.size()) {
    E e = q.front();
    q.pop();
    if (visit[e.first] != INF)
      continue;
    visit[e.first] = e.second;

    int len = G[e.first][0];
    for (int i = 1; i <= len; i++) {
      q.push(E(G[e.first][i], e.second + 1));
    }
  }

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

C++: /* ALDS1_11_C: Breadth First Search */

#include <iostream>
#include <vector>
#include <queue>
using std::vector;
using std::queue;

// 頂点0から各頂点への最短距離を paths に格納する。
void minimum_paths(int n_v, vector<int> graph[], int paths[]) {
    queue<int> visits;
    int found[n_v];
    std::fill_n(found, n_v, 0);
    found[0] = 1;

    visits.push(0);
    while(! visits.empty()) {
        int id = visits.front();
        visits.pop();
        vector<int> adjs = graph[id];
        for (int i = 0; i < adjs.size(); i++) {
            int adj = adjs[i];
            if (! found[adj]) {
                found[adj] = 1;
                paths[adj] = paths[id] + 1;
                visits.push(adj);
            }
        }
    }

    // 辿りつけなかった頂点までの距離は -1 とする。
    for (int i = 1; i < n_v; i++) {
        if (paths[i] == 0) {
            paths[i] = -1;
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    int n_v;
    std::cin >> n_v;

    int id, adj, n_adjs;
    vector<int> *graph = new vector<int>[n_v];
    for (int i = 0; i < n_v; i++) {
        std::cin >> id >> n_adjs;
        for (int j = 0; j < n_adjs; j++) {
            std::cin >> adj;
            graph[id - 1].push_back(adj - 1);
        }
    }

    int paths[n_v];
    std::fill_n(paths, n_v, 0);
    minimum_paths(n_v, graph, paths);

    for (int i = 0; i < n_v; i++) {
        std::cout << i + 1 << " " << paths[i] << "\n";
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

const int MAX = 128;

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

void solve(void){
	ENJYU;
	int N;
	cin >> N;
	rep(N, i) {
		int u, k;
		cin >> u >> k;
		u--;
		rep(k, i) {
			int v;
			cin >> v;
			v--;
			adj[u].pb(v);
		}
	}
	memset(d, -1, sizeof(d));
	queue <int> q;
	q.push(0);
	d[0] = 0;
	while (!q.empty()) {
		int n = q.front();
		q.pop();
		for (int nv : adj[n]) {
			if (d[nv] >= 0) {
				//d[nv] = min(d[nv], d[n] + 1);
				continue;
			}
			d[nv] = d[n] + 1;
			q.push(nv);
		}
	}

	rep(N, i) cout << i+1 << ' ' << d[i] << endl;

}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}

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

const int N =100;
const int inf = 0x3f3f3f3f;

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

void bfs(int s){
    queue<int> q;
    q.push(s);
    for(int i=0;i<n;i++) d[i] = inf;
    d[s] = 0;
    int u;
    while(!q.empty()){
        u = q.front();q.pop();
        for(int v=0;v<n;v++){
            if(M[u][v]==0) continue;
            if(d[v]!=inf) continue;
            d[v] = d[u]+1;
            q.push(v);
        }
    }
}

int main(){
    //freopen("a.txt","r",stdin);
    int u,v,k;
    memset(M,0,sizeof(M));
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&u);
        u--;
        scanf("%d",&k);
        for(int j=0;j<k;j++){
            scanf("%d",&v);
            v--;
            M[u][v] = 1;
        }
    }
    bfs(0);
    for(int i=0;i<n;i++){
        printf("%d %d\n",i+1,d[i]);
    }
    return 0;
}