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;
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 <stdio.h>
#include <iostream>
using namespace std;

class Adjl{
private:
	int u;
	int k;
	Adjl **next;
	int check_id;
public:
	Adjl(int u);
	~Adjl(){ if (next != NULL) delete[] next; }
	void setV(int k, Adjl *vk);
	int breadthFirstSearch(int id);
	void print(int id);
};

Adjl::Adjl(int u)
{
	this->u = u;
	k = 0;
	next = NULL;
	check_id = 0;
}

void Adjl::setV(int k, Adjl *vk)
{
	if (this->k == 0) next = new Adjl*[k];
	next[this->k] = vk;
	(this->k)++;
}

int Adjl::breadthFirstSearch(int id)
{
	if (id == check_id) return -1;
	else check_id = id;

	if (id == u) {
		return 0;
	}

	for (int i = 0; i < k; i++) {
		if (id == next[i]->u) {
			return 1;
		}
	}
	int d_min = -1;
	for (int i = 0; i < k; i++) {
		int d = next[i]->breadthFirstSearch(id);
		if (d == -1) continue;
		if (d_min == -1) d_min = d + 1;
		else d_min = min(d_min, d + 1);
	}
	return d_min;
}

void Adjl::print(int id)
{
	cout << id << " " << breadthFirstSearch(id) << endl;
}

int main(void)
{
	int n;
	cin >> n;
	
	Adjl **adjl = new Adjl*[n];
	int check[100] = {0};

	for (int i = 0; i < n; i++) {
		int u, k;
		cin >> u >> k;
		if (check[i] == 0) {
			adjl[i] = new Adjl(u);
			check[i] = 1;
		}
		check[i] = 1;
		for (int j = 0; j < k; j++) {
			int vk;
			cin >> vk;
			if (check[vk - 1] == 0) {
				adjl[vk - 1] = new Adjl(vk);
				check[vk - 1] = 1;
			}
			adjl[i]->setV(k, adjl[vk - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		adjl[0]->print(i);
	}

	delete[] adjl;
	return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define int ll

int g[105][105], d[105], n;

void bfs() {
  queue<int> q;
  q.push(0);
  fill(d, d + n, -1);
  d[0] = 0; 

  while(!q.empty()) {
    int que = q.front();
    q.pop();
    for (int i = 0; i < n; i++) {
      if (g[que][i] && d[i] == -1) {
        d[i] = d[que] + 1;
        q.push(i);
      }
    }
  }
}

signed main(void) {
  int u, k, v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v;
      g[u-1][v-1] = 1;
    }
  }

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

C++: #include<iostream>
#include<algorithm>
#include<queue>
#define MAX 101
using namespace std;
int n,d[MAX],color[MAX],A[MAX][MAX];
queue<int> Number_of_vertices;
void input(void){
    cin >> n;
    for(int i=0;i<n;i++){
        d[i] = -1;
        color[i] = 0;
        A[i][i] = 0;
    }
    for(int i=0;i<n;i++){
        int id,adj;
        cin >> id >> adj;
        id--;
        if(i==0){
            Number_of_vertices.push(id);
            d[id] = 0;
            color[id] = 1;
        }
        for(int j=0;j<adj;j++){
            int k;
            cin >> k;
            A[id][--k] = 1;
        }
    }
}
void Breadth_First_Search(void){
    while(!Number_of_vertices.empty()){
        int u = Number_of_vertices.front();
        Number_of_vertices.pop();
        for(int i=0;i<n;i++){
            if(color[i]==0 && A[u][i]==1){
                d[i] = d[u] + 1;
                color[i] = 1;
                Number_of_vertices.push(i);
            }
        }
    }
}
void print(void){
    for(int i=0;i<n;i++){
        cout << i + 1 << ' ' << d[i] << endl;
    }
}
int main(void){
    input();
    Breadth_First_Search();
    print();
    return 0;
}

C++: #include<stdio.h>
#define N 101
#define WHITE 2
#define GRAY 1
#define BLACK 0
#define INF 1000000

 
int n, h= 0, t = 0;
int col[N], d[N], a[N][N], e[N]; 

int Que[N];



 int Dequeque(){
  h++;
  return Que[h - 1];
}
void Enqueque(int b){
  Que[t] = b;
  t++;
}
 
void BFS(int s){
  int i, u, next;
  for(i = 1; i < n+1; i++){
      col[i] = WHITE;


      d[i] = -1;
  }
 
  col[s] = GRAY;
  d[s] = 0;
  Enqueque(s);
 
  while(h != t){
    u = Dequeque();
    for(i = 1; i <= n; i++){
      if((col[i] == WHITE) && (a[u][i] == 1)){


    col[i] = GRAY;
    d[i] = d[u] + 1;
    Enqueque(i);
      }
    }
   

    col[u] = BLACK;
  }
}
 

  
main(){
 
  scanf("%d", &n);
 
  int i, u, j,y=1;



  /*配列の初期化*/
  for(i = 1; i < n+1; i++){
    for(j = 1; j < n+1; j++){
     

 a[i][j] = 0;

    }
  }

  int x; 
  for(i = 1; i < n+1; i++){
    scanf("%d%d", &u, &e);
    for(j = 1; j <= e; j++){
      scanf("%d", &x);
      a[i][x] = 1;
    }
  }
 
  BFS(y);
   
  for(i = 1; i < n+1; i++){
    printf("%d ", i);
    printf("%d\n", d[i]);
  }
  return 0;
}