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 <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

typedef struct edge_def {
  int from;
  int to;
  int cost;

  edge_def(int _from, int _to, int _cost) {
    from = _from;
    to   = _to;
    cost = _cost;
  }
} edge;

const int INF = INT_MAX;
const int V = 100;
int n, t;
vector<edge> es[V];
int d[V];

void dijkstra(int s) {
  int i;
  priority_queue<PII, vector<PII>, greater<PII > > q;
  for (i=0; i<n; i++) d[i] = INF;
  d[s] = 0;
  q.push(PII(0, s));

  while (q.size()) {
    PII p = q.top();
    q.pop();
    int v = p.second;

    if (d[v] < p.first) continue;

    for (i=0; i<(int)es[v].size(); i++) {
      edge e = es[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost; 
        q.push(PII(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int i, j;
  memset(d, 0xff, sizeof(d));
  for (i=0; i<V; i++) es[i].clear();

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

  dijkstra(0);

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

	return 0;
}

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

using namespace std;

static const int N = 100;
static const int NEW = 0;
static const int VISITING = 1;
static const int OLD = 2;

int n, m[N][N] = {0}, t;
int color[N] = { 0 }, t1[N] = { 0 }, t2[N] = {0};

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

int nxt(int idx)
{
	for (int i = 0; i < n; ++i)
	{
		if (1 == m[idx][i] && color[i] == NEW)
			return i;
	}
	return -1;
}

void dfs_visit(int idx)
{
	stack<int> s;
	s.push(idx);
	color[idx] = VISITING;
	t1[idx] = t++;


	while (!s.empty())
	{
		int u = s.top();
		int v = nxt(u);
		if (-1 != v)
		{
			color[v] = VISITING;
			t1[v] = t++;
			s.push(v);
		}
		else
		{
			s.pop();
			color[u] = OLD;
			t2[u] = t++;
		}
	}
}

void dfs()
{
	t = 1;

	for (int i = 0; i < n; ++i)
		color[i] = NEW;

	for (int i = 0; i < n; ++i)
	{
		if (color[i] == NEW)
			dfs_visit(i);
	}

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

int dist[N];

void bfs(int idx)
{
	queue<int> q;
	q.push(idx);

	for (int i = 0; i < n; ++i)
		dist[i] = INT_MAX;
	dist[idx] = 0;

	int u;
	while (!q.empty())
	{
		u = q.front(); q.pop();
		for (int i = 0; i < n; ++i)
		{
			if (m[u][i] == 0) continue;
			if (dist[i] != INT_MAX) continue;
			dist[i] = dist[u] + 1;
			q.push(i);
		}
	}
	for (int i = 0; i < n; ++i)
	{
		cout << i + 1 << " " <<( (dist[i] == INT_MAX) ? (-1) : dist[i]) << endl;
	}
}


int main()
{
	getMatrix();
	bfs(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() {
  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 <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 100;
vector<int> G[maxn + 5];
//int book[maxn + 5][maxn + 5];
int n;
int far[maxn + 5];


int bfs(int vertex) {
    queue<int> q;
    int book[maxn + 5] = {};
    q.push(vertex);
    book[vertex] = 1;
    int min = 0;
    if(vertex == 1) return 0;

    while(!q.empty()) {
        int now = q.front();
        q.pop();

        int len = G[now].size();
        for(int i = 0; i < len; i++) {
            if(book[G[now][i]]) continue;

            if(G[now][i] == 1) return min + 1;

            book[G[now][i]] = 1;
            q.push(G[now][i]);
        }
        min++;
    }
    return -1;
}



int main() {
    scanf("%d", &n);

    for(int i = 1; i <= n; i++) {
        int num;
        scanf("%d", &num);
        int count;
        scanf("%d", &count);


        while(count--) {
            int temp; scanf("%d", &temp);
            G[num].push_back(temp);
            G[temp].push_back(num);
        }
    }

    for(int i = 1; i <= n; i++) {
        far[i] = bfs(i);
    }

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

    return 0;
}