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 <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>

using namespace std;
int n,m,l,p;
int 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] < 1 && 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++) {
		D[i] = -1;
		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 <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

vector<int> g[101];
bool used[101];
int ans[101];
void bfs(int sr){
    queue<pint> q;q.push({sr,0});
    used[sr]=true;
    while(!q.empty()){
        pint pi=q.front();q.pop();
        ans[pi.first]=pi.second;
        rep(i,g[pi.first].size()){
            int nx=g[pi.first][i];
            if(!used[nx]){
                used[nx]=true;
                q.push({nx,pi.second+1});
            }
        }
    }
}
int main(){
    int n,u,k,v;
    cin>>n;
    rep(i,n){
        cin>>u>>k;
        --u;
        rep(j,k){
            cin>>v;
            --v;
            g[u].pb(v);
        }
    }
    memset(ans,-1,sizeof(ans));
    bfs(0);
    rep(i,n) cout<<i+1<<" "<<ans[i]<<endl;
    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define MAX_N 100

int n,time,head,tail;
int *queue;
int *dist;
int *color;
int* matrix[MAX_N];

void enqueue(int);
int dequeue(void);
void bfs(void);

int main(){
  int i,j,u,k,v;

  scanf("%d",&n);

  queue = malloc(n * sizeof(int));
  color = malloc(n * sizeof(int));
  dist = malloc(n * sizeof(int));

  for(i = 0 ; i < n ; i++){
    matrix[i] = malloc(n * sizeof(int));
  }
 for(i = 0 ; i < n ; i++){
   dist[i] = -1;
 }
 for(i = 0 ; i < n ; i++){
   for(j = 0 ; j < n ; j++){
     matrix[i][j] = 0;
   }
 }

 for(i = 0 ; i < n ; i++){
    scanf("%d",&u); u--;
    scanf("%d",&k);
    for(j = 0 ; j < k ; j++){
      scanf("%d",&v); v--;
      matrix[u][v] = 1;
    }
  }

  bfs();
  for(i = 0 ; i < n ; i++){
    printf("%d %d\n",i+1,dist[i]);
  }

  for(i = 0 ; i < n ; i++){
    free(matrix[i]);
  }
  free(queue);
  free(dist);
  free(color);
  return 0;
}

void bfs(){
  int u,i;

  head = tail = 0;
  color[0] = GRAY;
  dist[0] = 0;
  enqueue(0);

  while(head != tail){
    u = dequeue();
    for(i = 0 ; i < n ; i++){
      if(matrix[u][i] == 1 && color[i] == WHITE){
        color[i] = GRAY;
        dist[i] = dist[u] + 1;
        enqueue(i);
      }
    }
    color[u] = BLACK;
  }
}

void enqueue(int q){
  queue[tail] = q;
  if(++tail == n+1){ tail = 0; cout<<'a'<<endl;}
}

int dequeue(){
  int v = queue[head];
  if(++head == n+1) head = 0;
  return v;
}

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

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

    vector<int> dist(n + 1, -1);
    queue<int> que;

    dist[1] = 0;
    que.push(1);

    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);
        }
    }
    
    for (int i = 1; i <= n; i++)
    {
        cout << i << " " << dist[i] << endl;
    }
    
}

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

  int a,b,tmp;
  cin>>n;
  for(int i=0;i<n;i++){
    if(i) f[i]=1000;
    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++) if(f[i]==1000) f[i]=-1;
  for(int i=0;i<n;i++) cout<<i+1<<" "<<f[i]<<endl;

  return 0;

}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
// using ll = long long;
// using P = pair<int, int>;

bool visited[105];
int ans[105];
vector<vector<int> > v(105);

int main() {
    int n;
    cin >> n;
    
    int x, y;
    rep(i, n){
        cin >> x >> y;
        rep(j, y){
            int t;
            cin >> t;
            v[x-1].push_back(t-1);
        }
    }
    
    rep(i, 105){
        visited[i] = false;
        ans[i] = -1;
    }
    
    queue<pair<int, int> > q;
    q.push(make_pair(0, 0));
    visited[0] = true;
    
    while(!q.empty()){
        int node = q.front().first;
        int t_found = q.front().second;
        q.pop();
        rep(i, (int)v[node].size()){
            if(!visited[v[node][i]]){
                q.push(make_pair(v[node][i], t_found+1));
                visited[v[node][i]] = true;
            }
        }
        ans[node] = t_found; 
    }
    
    rep(i, n){
        cout << i+1 << " " << ans[i] << endl;
    }
    
    return 0;
}