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 <stdio.h>


int map_max;
int map[110][110];
int rows;
int cols[110];
int howfar[100];
int far;
int visited[100];

void get_map();
void print_map();
int search(int node, int far);
void print_result();
void check_result();

int main()
{

  get_map();
  //print_map();
  search(1, 0);
  check_result();
  print_result();
  return 0;
}

void check_result()
{
  for(int i=1;i<map_max;i++)
    {
      if(howfar[i]==0)
	{
	  howfar[i]=-1;
	}
    }
}

void print_result()
{
  for(int i=0;i<map_max;i++)
    {
      std::cout << i+1 << " " << howfar[i] << std::endl;
    }
}

void get_map()
{

  int k;
  scanf("%d", &map_max);//<100

  for(int i=0;i<map_max;i++)
    {
      scanf("%d", &k);
      scanf("%d", &cols[i]);
      for(int j=0;j<cols[i];j++)
	{
	  map[i][j]=0;
	  scanf("%d", &map[i][j]);
	}
    }

  
  return ;
}

void print_map()
{
  for(int i=0;i<map_max;i++)
    {
      for(int j=0;j<cols[i];j++)
	{
	  printf("%d", map[i][j]);
	}
      puts("");
    }
return ;
}
int search(int node, int far)
{
  if(visited[node-1] == 1)
    {
      if(howfar[node-1]>far)
	{
	  howfar[node-1]=far;
	}
      return -1;
    }
  else
    {
      howfar[node-1]=far;
      visited[node-1]=1;
    }

  for(int i=0;i<cols[node-1];i++)
    {
      //if(map[node-1][i]!=0)
	{
	  search(map[node-1][i], far+1);
	}
    }
  return -1;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>

using namespace std;

const int MAX_N = 101;
const int DUMMY = 10000;

int N, u, k, v = 0;

vector<int>V [MAX_N];
int d [MAX_N];
bool visited [MAX_N];

bool bfs(int u, int depth)
{
    for (int i = 0; i < V[u].size(); ++i) {
        if(V[u][i] == 1) continue;
        bfs(V[u][i] - 1, depth + 1);
    }
        
    
    d[u] = min(d[u], depth);
    
    if (V[u].size() == 0 || u == 0)
        return true;
}

int main()
{
    //initialize
    for (int i = 0; i < MAX_N; ++i) {
        d[i]   = DUMMY;
        visited[i] = false;
    }
    
    //input
    scanf("%d", &N);
    for (int i = 0; i < N; ++i) {
      scanf("%d %d", &u, &k);
      for (int j = 0; j < k; ++j) {
          scanf("%d", &v);
          V[i].push_back(v);
      }
      sort(V[i].begin(), V[i].end());
    }

    //serach
    bfs(1-1, 0);
    
    //output
    for (int i = 0; i < N; ++i) {
      if(d[i] == DUMMY) cout << i+1 << ' ' << -1 << endl;
      else cout << i+1 << ' ' << d[i] << endl;
    }

    return 0;
}

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() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      M[i][j] = 0;
    }
  }

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

  bfs(0);

  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
static const int N = 100;

int n;
int M[N][N];
int vis[N];
int seq[N];

void bfs_print() {
	for (int i = 0; i < n; i++) {
		cout << i + 1 << ' ' << seq[i]<<endl;
	}
}

int main() {
	cin >> n;
	int u, k;
	int index;

	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;
		for (int j = 0; j < k; j++) {
			cin >> index;
			M[u - 1][index - 1] = 1;
		}
	}
	for (int i = 0; i < n; i++) {
		seq[i] = 0;
		vis[i] = 0;
		if(i >0) seq[i] = -1;
	}

	queue<int> q;
	seq[0] = 0;
	int t;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (M[i][j] == 1) {
				q.push(j);
			}
		}
		while (not q.empty()) {
			if (vis[q.front()] == 0) {
				seq[q.front()] = seq[i] + 1;
				vis[q.front()] = 1;
			}
			q.pop();
		}
	}

	bfs_print();
}
//
//void dfs_visit(int u) {
//	color[u] = GRAY;
//	d[u] = ++tt;
//	for (int v = 0; v < n; v++) {
//		if (M[u][v] == 1 && color[v] == WHITE) {
//			dfs_visit(v);
//		}
//	}
//	color[u] = BLACK;
//	f[u] = ++tt;
//}
//
//void dfs_print() {
//	for (int i = 0; i < n; i++) color[i] = WHITE;
//	tt = 0;
//	for (int i = 0; i < n; i++) {
//		if (color[i] == WHITE) {
//			dfs_visit(i);
//		}
//	}
//
//	for (int i = 0; i < n; i++) {
//		cout << i + 1 << ' ' << d[i] << ' ' << f[i] << endl;
//	}
//}
//