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 <queue>
using namespace std;

const int MAX_N = 100;
int to[MAX_N][MAX_N];
int ans[MAX_N];
bool visited[MAX_N];

int main() {
  int N;
  cin >> N;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      to[i][j] = -1;
    }
    ans[i] = -1;
    visited[i] = false;
  }
  for (int i=0; i<N; i++) {
    int tmp, n;
    cin >> tmp >> n;
    for (int j=0; j<n; j++) {
      int t;
      cin >> t; t--;
      to[i][j] = t;
    }
  }

  queue<pair<int, int> > que;
  que.push(make_pair(0, 0));
  while (!que.empty()) {
    pair<int, int> tmp = que.front(); que.pop();
    int x = tmp.first, depth = tmp.second;
    if (ans[x] == -1) {
      ans[x] = depth;
    } else {
      continue;
    }
    for (int i=0; to[x][i] != -1; i++) {
      que.push(make_pair(to[x][i], depth + 1));
    }
  }

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

C++: #include<stdio.h>
#include<stack>
#include<iostream>
#include<string>
using namespace std;

int n = 0;//pointing at number of line
int u = 0;//number of top
int k[101];//output order(connect)
int v[101][101];//number of top
int Short[101] = { 0 };//Short(d[v])
//into n = 0;//element count
int p = 0;//Pointer
int fptr = 0;
int lptr = 0;
int x1[10000];
int y1[10000];
void Enqueue(int y, int x)
{
	x1[lptr] = x;//top of number
	y1[lptr] = y;//shortest
	lptr = (lptr + 1) % 10000;
}
void bfs(int l) {
	int i;
	while (fptr!=lptr) {
		l=x1[fptr];
		p=y1[fptr];
		fptr = (fptr + 1) % 10000;
		if(Short[l]!=-1){
			continue;
		}
		for(i=0;i<k[l];i++){
			if(v[l][i]){
				Enqueue(p+1,v[l][i]);
			}
		}
		//Enqueue(v[l][in]);
		Short[l]=++p;
	}
}
int main()
{
	int i;
	int j;
	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> u;
		cin >> k[u];
		for (j = 0; j < k[u]; j++) {
			cin >> v[u][j];
		}
	}
	Enqueue(-1,1);
	for(i=0;i<n+1;i++){
		Short[i]=-1;
	}
	bfs(0);
	
	for (i = 1; i < n + 1; i++) {
		cout << i << " " << Short[i] << endl;
	}
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int d[128][128],solve[128];
int main(){
      //input
      int n,m,row,t;
      queue<int> que;
      cin >> n;
      for(int i= 0;i<n;i++){
            cin >>row>>m;
            for(int j = 0;j<m;j++){
                  cin>>t;
                  d[row][t] = 1;
            }
      }
      //debug
      //for(int i = 0;i<=n;i++){
      //for(int j = 0;j<=n;j++)cout << d[i][j] << ' ';cout << endl;}

      //solve
      fill(solve,solve+n+2,-1);
      que.push(1);
      solve[1] = 0;
      while(!que.empty())
      {
            int p = que.front();
            que.pop();
            for(int i = 1;i<=n;i++)
            {
                  if(d[p][i] && solve[i]==-1)
                  {
                        que.push(i);
                        solve[i] = solve[p] + 1;
                  }
            }
      }
      for(int i = 1;i<=n;i++)
            cout << i << ' '<< solve[i]<<endl;


}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1 << 21);

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] = INFTY;
  }
  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] != INFTY) continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for (int i = 0; i < n; i++) {
    cout << i + 1 << " " << ((d[i] == INFTY) ? (-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++) {
      M[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    u--;
    for (int j = 0; j < k; j++) {
      cin >> v;
      v--;
      M[u][v] = 1;
    }
  }

  bfs(0);

  return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
int d[128][128],solve[128];
int main(){
      int n,m,row,t;
      queue<int> que;
      cin >> n;
      for(int i= 0;i<n;i++){
            cin >>row>>m;
            for(int j = 0;j<m;j++){
                  cin>>t;
                  d[row][t] = 1;
            }
      }
      fill(solve,solve+n+2,-1);
      que.push(1);
      solve[1] = 0;
      while(!que.empty()){
            int p = que.front();
            que.pop();
            for(int i = 1;i<=n;i++){
                  if(d[p][i] && solve[i]==-1){
                        que.push(i);
                        solve[i] = solve[p] + 1;
                  }
            }
      }
      for(int i = 1;i<=n;i++)cout << i << ' '<< solve[i]<<endl;
}

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

int n, u, k, v;
int a[100][100] = {{0}};
int b[100] = { - 1 };
queue<int> q;

int main() {
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		cin >> u >> k;
		for ( j = 0; j < k; j++ ) {
			cin >> v;
			a[u - 1][v - 1] = 1;
		}
	}
	b[0] = 0;
	q.push( 0 );
	while ( ! q.empty() ) {
		u = q.front();
		for ( int i = 0; i < n; i++ ) {
			if ( a[u][i] == 1 && b[i] == -1 ) {
				b[i] = b[u] + 1;
				q.push( i );
			}
		}
		q.pop();
	}
	for ( int i = 0; i < n; i++ )
		cout << ( i + 1 ) << " " << b[i] << endl;
}